001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 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): Fran?ois Exertier
023: *
024: * --------------------------------------------------------------------------
025: * $Id: MsgReceptor.java 4618 2004-04-19 06:39:30Z benoitf $
026: * --------------------------------------------------------------------------
027: */package jms;
028:
029: import javax.jms.Connection;
030: import javax.jms.ConnectionFactory;
031: import javax.jms.Message;
032: import javax.jms.MessageConsumer;
033: import javax.jms.MessageListener;
034: import javax.jms.ObjectMessage;
035: import javax.jms.Session;
036: import javax.jms.TextMessage;
037: import javax.jms.Topic;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import javax.naming.NamingException;
041:
042: /**
043: * @author JOnAS team
044: */
045: public class MsgReceptor {
046:
047: private static Context ictx = null;
048:
049: private static ConnectionFactory cf = null;
050:
051: private static Topic topic = null;
052:
053: // JNDI name of the connection factory
054: private static String conFactName = "JCF";
055:
056: // JNDI name of the Topic
057: private static String topicName = "sampleTopic";
058:
059: public static void main(String[] arg) {
060:
061: // Get InitialContext
062: try {
063: ictx = new InitialContext();
064: // lookup the TopicConnectionFactory through its JNDI name
065: cf = (ConnectionFactory) ictx.lookup(conFactName);
066:
067: System.out.println("JMS client: cf = " + cf.toString());
068:
069: // lookup the Topic through its JNDI name
070: topic = (Topic) ictx.lookup(topicName);
071: System.out.println("JMS Topic topic = " + topic.toString());
072: } catch (NamingException e) {
073: e.printStackTrace();
074: System.exit(2);
075: }
076:
077: try {
078: Connection tc = cf.createConnection();
079: System.out.println("JMS client: tc = " + tc.toString());
080: Session session = tc.createSession(false,
081: Session.AUTO_ACKNOWLEDGE);
082: MessageConsumer mc = session.createConsumer(topic);
083: /* create and set a MessageListener */
084: MyListenerSimple listener = new MyListenerSimple();
085: mc.setMessageListener(listener);
086:
087: System.out.println("JMS client: Waiting for messages ...");
088: tc.start();
089:
090: System.in.read();
091:
092: session.close();
093: tc.close();
094: } catch (Exception e) {
095: System.out
096: .println("Exception in message reception operations");
097: e.printStackTrace();
098: }
099:
100: }
101: }
102:
103: class MyListenerSimple implements MessageListener {
104:
105: MyListenerSimple() {
106: }
107:
108: public void onMessage(Message msg) {
109: try {
110: if (msg == null) {
111: System.out.println("Message: message null ");
112: } else {
113: if (msg instanceof ObjectMessage) {
114: String m = (String) ((ObjectMessage) msg)
115: .getObject();
116: System.out
117: .println("JMS client: received message ======> "
118: + m);
119: } else if (msg instanceof TextMessage) {
120: String m = ((TextMessage) msg).getText();
121: System.out
122: .println("JMS client: received message ======> "
123: + m);
124: }
125: }
126: } catch (Exception exc) {
127: System.out.println("Exception caught :" + exc);
128: exc.printStackTrace();
129: }
130: }
131: }
|