1 22 23 package hero.client.events; 24 25 import javax.jms.TopicConnectionFactory ; 26 import javax.jms.Topic ; 27 import javax.jms.TopicConnection ; 28 import javax.jms.TopicSession ; 29 import javax.jms.TopicSubscriber ; 30 import javax.jms.TopicPublisher ; 31 import javax.jms.TextMessage ; 32 import javax.jms.ObjectMessage ; 33 import javax.jms.JMSException ; 34 import javax.jms.Session ; 35 import javax.jms.MessageListener ; 36 import javax.jms.Message ; 37 import javax.naming.NamingException ; 38 import javax.naming.Context ; 39 import javax.naming.InitialContext ; 40 import java.io.InputStreamReader ; 41 import java.util.Hashtable ; 42 import java.util.Enumeration ; 43 44 45 public class JMSServicesClient { 46 47 private static JMSServicesClient jms = null; 49 private static String topicName = "testTopic"; 50 private static Context jndiContext = null; 51 private static TopicConnectionFactory topicConnectionFactory = null; 52 private static TopicSession topicSession = null; 53 private static Topic topic = null; 54 private static TopicConnection topicConnection = null; 55 private static TopicSubscriber topicSubscriber = null; 56 private static TopicPublisher topicPublisher = null; 57 private static TextMessage message = null; 58 private static ObjectMessage omessage = null; 59 private static InputStreamReader inputStreamReader = null; 60 61 private JMSServicesClient() throws Exception { 62 63 66 67 try { 68 69 jndiContext = new InitialContext (); 70 } catch (NamingException e) { 71 throw new Exception (e.getMessage(),e); 72 } 73 74 78 79 80 try { 81 topicConnectionFactory = (TopicConnectionFactory ) jndiContext.lookup("JTCF"); 82 topic = (Topic ) jndiContext.lookup(topicName); 83 } catch (NamingException e) { 84 System.out.println("JNDI lookup failed: " 85 + e.toString()); 86 throw new Exception (e.getMessage(),e); 87 } 89 90 95 96 97 try { 98 topicConnection = topicConnectionFactory.createTopicConnection(); 99 topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); 100 } catch (JMSException e) { 101 System.out.println("Exception occurred: " + e.toString()); 102 } 103 104 } 105 106 110 111 public static JMSServicesClient getInstance() throws Exception { 112 if (jms == null) { 113 jms = new JMSServicesClient(); 114 } 115 return jms; 116 117 } 118 119 124 125 public void createSubscription(MessageListener listener, String evt) throws Exception { 126 127 try { 128 topicSubscriber = topicSession.createSubscriber(topic, evt,false); 129 topicSubscriber.setMessageListener(listener); 130 topicConnection.start(); 131 } catch (JMSException e) { 132 System.out.println("Exception occurred: " 133 + e.toString()); 134 } 135 } 136 137 138 public static Hashtable getEvt(Message message) throws JMSException 139 { 140 Enumeration keys = message.getPropertyNames(); 141 Hashtable evt = new Hashtable (); 142 143 while (keys.hasMoreElements()) 144 { 145 String key = (String )keys.nextElement(); 146 evt.put(key,message.getObjectProperty(key)); 147 } 148 return evt; 149 } 150 151 public static java.io.Serializable getEvtObject(Message message) throws JMSException 152 { 153 return (((ObjectMessage )message).getObject()); 154 } 155 156 public void closeConnection() { 157 158 if (topicConnection != null) { 159 try { 160 topicConnection.close(); 161 } catch (JMSException e) {} 162 } 163 } 164 165 } 166 | Popular Tags |