001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): ____________________________________.
022: * Contributor(s): ______________________________________.
023: *
024: * --------------------------------------------------------------------------
025: * $Id: MdbClient.java 9197 2006-07-17 14:21:14Z pelletib $
026: * --------------------------------------------------------------------------
027: */
028:
029: // MdbClient.java
030: // mini Client for accessing bean Mdb
031: package newsamplemdb;
032:
033: import javax.naming.Context;
034: import javax.naming.InitialContext;
035: import javax.naming.*;
036: import javax.jms.*;
037:
038: /**
039: *
040: */
041: public class MdbClient {
042:
043: static Context ictx = null;
044: static TopicConnectionFactory tcf = null;
045: static TopicPublisher tp = null;
046: static Topic topic = null;
047: // JNDI name of the Topic
048: static String topicName = "mdbTopic";
049: // JNDI name of the default connection factory
050: static String conFactName = "JTCF";
051:
052: public static void main(String[] arg) {
053: // Get InitialContext
054: try {
055: ictx = new InitialContext();
056: // lookup the TopicConnectionFactory through its JNDI name
057: tcf = (TopicConnectionFactory) ictx.lookup(conFactName);
058: System.out.println("JMS client: tcf = " + tcf);
059: // lookup the Topic through its JNDI name
060: topic = (Topic) ictx.lookup(topicName);
061: System.out.println("JMS client: topic = " + topic);
062: } catch (NamingException e) {
063: e.printStackTrace();
064: System.exit(2);
065: }
066:
067: TopicConnection tc = null;
068: TopicSession session = null;
069: try {
070: tc = tcf.createTopicConnection();
071: System.out.println("JMS client: tc = " + tc.toString());
072: session = tc.createTopicSession(false,
073: Session.AUTO_ACKNOWLEDGE);
074: tp = session.createPublisher(topic);
075: } catch (Exception e) {
076: e.printStackTrace();
077: System.exit(2);
078: }
079:
080: // publish 10 messages to the topic
081: int nbmess = 100;
082: try {
083: TextMessage message;
084: for (int i = 0; i < nbmess; i++) {
085: message = session.createTextMessage();
086: message.setText("Message" + i);
087: tp.publish(message);
088: if (i % 10 == 0) {
089: Thread.sleep(1000);
090: }
091: System.out.println("Msg n " + i);
092:
093: }
094: session.close();
095: tc.close();
096: } catch (Exception e) {
097: e.printStackTrace();
098: System.exit(2);
099: }
100: System.out.println("MDBsample is Ok");
101: }
102: }
|