KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > client > events > JMSServicesClient


1 /*
2 *
3 * JMSServicesClient.java -
4 * Copyright (C) 2004 Ecoo Team
5 * valdes@loria.fr
6 *
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */

22
23 package hero.client.events;
24
25 import javax.jms.TopicConnectionFactory JavaDoc;
26 import javax.jms.Topic JavaDoc;
27 import javax.jms.TopicConnection JavaDoc;
28 import javax.jms.TopicSession JavaDoc;
29 import javax.jms.TopicSubscriber JavaDoc;
30 import javax.jms.TopicPublisher JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32 import javax.jms.ObjectMessage JavaDoc;
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.Session JavaDoc;
35 import javax.jms.MessageListener JavaDoc;
36 import javax.jms.Message JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import javax.naming.Context JavaDoc;
39 import javax.naming.InitialContext JavaDoc;
40 import java.io.InputStreamReader JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.Enumeration JavaDoc;
43
44
45 public class JMSServicesClient {
46
47     private static JMSServicesClient jms = null; // Singleton pattern
48

49     private static String JavaDoc topicName = "testTopic";
50     private static Context JavaDoc jndiContext = null;
51     private static TopicConnectionFactory JavaDoc topicConnectionFactory = null;
52     private static TopicSession JavaDoc topicSession = null;
53     private static Topic JavaDoc topic = null;
54     private static TopicConnection JavaDoc topicConnection = null;
55     private static TopicSubscriber JavaDoc topicSubscriber = null;
56     private static TopicPublisher JavaDoc topicPublisher = null;
57     private static TextMessage JavaDoc message = null;
58     private static ObjectMessage JavaDoc omessage = null;
59     private static InputStreamReader JavaDoc inputStreamReader = null;
60
61     private JMSServicesClient() throws Exception JavaDoc{
62
63         /*
64          * Create a JNDI InitialContext object if none exists yet.
65          */

66
67         try {
68
69         jndiContext = new InitialContext JavaDoc();
70         } catch (NamingException JavaDoc e) {
71         throw new Exception JavaDoc(e.getMessage(),e);
72         }
73
74         /*
75          * Look up connection factory and topic. If either does
76          * not exist, exit.
77          */

78
79
80         try {
81              topicConnectionFactory = (TopicConnectionFactory JavaDoc) jndiContext.lookup("JTCF");
82          topic = (Topic JavaDoc) jndiContext.lookup(topicName);
83         } catch (NamingException JavaDoc e) {
84             System.out.println("JNDI lookup failed: "
85                 + e.toString());
86         throw new Exception JavaDoc(e.getMessage(),e);
87         // System.exit(1);
88
}
89
90     /*
91      * Create connection.
92      * Create session from connection; false means session is
93      * not transacted.
94      */

95      
96
97     try {
98         topicConnection = topicConnectionFactory.createTopicConnection();
99         topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
100     } catch (JMSException JavaDoc e) {
101         System.out.println("Exception occurred: " + e.toString());
102     }
103
104     }
105     
106     /**
107      * Returns the instance of the JMSServices
108      * Implementation of the Singleton pattern.
109     */

110     
111     public static JMSServicesClient getInstance() throws Exception JavaDoc {
112         if (jms == null) {
113            jms = new JMSServicesClient();
114         }
115         return jms;
116
117     }
118
119     /**
120      * Create subscriber.
121      * Register message listener (TextListener).
122      * Receive text messages from topic.
123      */

124
125     public void createSubscription(MessageListener JavaDoc listener, String JavaDoc evt) throws Exception JavaDoc {
126
127     try {
128         topicSubscriber = topicSession.createSubscriber(topic, evt,false);
129         topicSubscriber.setMessageListener(listener);
130         topicConnection.start();
131     } catch (JMSException JavaDoc e) {
132       System.out.println("Exception occurred: "
133                  + e.toString());
134     }
135     }
136
137
138     public static Hashtable JavaDoc getEvt(Message JavaDoc message) throws JMSException JavaDoc
139     {
140     Enumeration JavaDoc keys = message.getPropertyNames();
141     Hashtable JavaDoc evt = new Hashtable JavaDoc();
142     
143     while (keys.hasMoreElements())
144     {
145         String JavaDoc key = (String JavaDoc)keys.nextElement();
146         evt.put(key,message.getObjectProperty(key));
147     }
148     return evt;
149     }
150
151     public static java.io.Serializable JavaDoc getEvtObject(Message JavaDoc message) throws JMSException JavaDoc
152     {
153     return (((ObjectMessage JavaDoc)message).getObject());
154     }
155     
156     public void closeConnection() {
157     
158     if (topicConnection != null) {
159         try {
160         topicConnection.close();
161         } catch (JMSException JavaDoc e) {}
162     }
163     }
164     
165 }
166
Popular Tags