001: /*
002: *
003: * JMSServicesClient.java -
004: * Copyright (C) 2004 Ecoo Team
005: * valdes@loria.fr
006: *
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: package hero.client.events;
024:
025: import javax.jms.TopicConnectionFactory;
026: import javax.jms.Topic;
027: import javax.jms.TopicConnection;
028: import javax.jms.TopicSession;
029: import javax.jms.TopicSubscriber;
030: import javax.jms.TopicPublisher;
031: import javax.jms.TextMessage;
032: import javax.jms.ObjectMessage;
033: import javax.jms.JMSException;
034: import javax.jms.Session;
035: import javax.jms.MessageListener;
036: import javax.jms.Message;
037: import javax.naming.NamingException;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import java.io.InputStreamReader;
041: import java.util.Hashtable;
042: import java.util.Enumeration;
043:
044: public class JMSServicesClient {
045:
046: private static JMSServicesClient jms = null; // Singleton pattern
047:
048: private static String topicName = "testTopic";
049: private static Context jndiContext = null;
050: private static TopicConnectionFactory topicConnectionFactory = null;
051: private static TopicSession topicSession = null;
052: private static Topic topic = null;
053: private static TopicConnection topicConnection = null;
054: private static TopicSubscriber topicSubscriber = null;
055: private static TopicPublisher topicPublisher = null;
056: private static TextMessage message = null;
057: private static ObjectMessage omessage = null;
058: private static InputStreamReader inputStreamReader = null;
059:
060: public JMSServicesClient() throws Exception {
061:
062: /*
063: * Create a JNDI InitialContext object if none exists yet.
064: */
065:
066: try {
067:
068: jndiContext = new InitialContext();
069: } catch (NamingException e) {
070: throw new Exception(e.getMessage(), e);
071: }
072:
073: /*
074: * Look up connection factory and topic. If either does
075: * not exist, exit.
076: */
077:
078: try {
079: topicConnectionFactory = (TopicConnectionFactory) jndiContext
080: .lookup("JTCF");
081: topic = (Topic) jndiContext.lookup(topicName);
082: } catch (NamingException e) {
083: System.out.println("JNDI lookup failed: " + e.toString());
084: throw new Exception(e.getMessage(), e);
085: // System.exit(1);
086: }
087:
088: /*
089: * Create connection.
090: * Create session from connection; false means session is
091: * not transacted.
092: */
093:
094: try {
095: topicConnection = topicConnectionFactory
096: .createTopicConnection();
097: topicSession = topicConnection.createTopicSession(false,
098: Session.AUTO_ACKNOWLEDGE);
099: } catch (JMSException e) {
100: System.out.println("Exception occurred: " + e.toString());
101: }
102:
103: }
104:
105: /**
106: * Returns the instance of the JMSServices
107: * Implementation of the Singleton pattern.
108: */
109:
110: public static JMSServicesClient getInstance() throws Exception {
111: if (jms == null) {
112: jms = new JMSServicesClient();
113: }
114: return jms;
115:
116: }
117:
118: /**
119: * Create subscriber.
120: * Register message listener (TextListener).
121: * Receive text messages from topic.
122: */
123:
124: public void createSubscription(MessageListener listener, String evt)
125: throws Exception {
126:
127: try {
128: topicSubscriber = topicSession.createSubscriber(topic, evt,
129: false);
130: topicSubscriber.setMessageListener(listener);
131: topicConnection.start();
132: } catch (JMSException e) {
133: e.printStackTrace();
134: System.out.println("Exception occurred: " + e.toString());
135: }
136: }
137:
138: public static Hashtable getEvt(Message message) throws JMSException {
139: Enumeration keys = message.getPropertyNames();
140: Hashtable evt = new Hashtable();
141:
142: while (keys.hasMoreElements()) {
143: String key = (String) keys.nextElement();
144: evt.put(key, message.getObjectProperty(key));
145: }
146: return evt;
147: }
148:
149: public static java.io.Serializable getEvtObject(Message message)
150: throws JMSException {
151: return (((ObjectMessage) message).getObject());
152: }
153:
154: public void closeConnection() {
155:
156: if (topicConnection != null) {
157: try {
158: topicConnection.close();
159: } catch (JMSException e) {
160: }
161: }
162: }
163:
164: }
|