1 packagech02.chat;2
3 importjava.io.Serializable;4
5 importjavax.jms.Connection;6 importjavax.jms.ConnectionFactory;7 importjavax.jms.Destination;8 importjavax.jms.JMSException;9 importjavax.jms.Message;10 importjavax.jms.MessageConsumer;11 importjavax.jms.MessageListener;12 importjavax.jms.MessageProducer;13 importjavax.jms.ObjectMessage;14 importjavax.jms.Session;15 /*本工具封装了*/
16
17
18 importjavax.jms.TextMessage;19 importjavax.jms.Topic;20 importjavax.jms.TopicConnection;21 importjavax.jms.TopicConnectionFactory;22 importjavax.jms.TopicPublisher;23 importjavax.jms.TopicSession;24 importjavax.jms.TopicSubscriber;25
26 importorg.apache.activemq.ActiveMQConnection;27 importorg.apache.activemq.ActiveMQConnectionFactory;28
29 public classJMSTopic {30 TopicConnectionFactory connectionFactory;31 //Connection :JMS 客户端到JMS Provider 的连接
32 TopicConnection connection = null;33 //用来发布的会话34
35 //TopicSession proSession = null;36 //2一个订阅会话37 //TopicSession conSession = null;
38 TopicSession session=null;39
40 //主题发布者
41 MessageProducer producer=null;42 //主题
43 MessageConsumer consumer=null;44
45
46 //Destination :消息的目的地;消息发送给谁.
47 Destination destination;48 //MessageProducer:消息发送者49
50 //默认构造函数,默认的连接activemq,可以写多个构造函数
51 publicJMSTopic()52 {53 connectionFactory = newActiveMQConnectionFactory(54 ActiveMQConnection.DEFAULT_USER,55 ActiveMQConnection.DEFAULT_PASSWORD,56 "tcp://localhost:61616");57 try{58 connection=connectionFactory.createTopicConnection();59 } catch(JMSException e) {60 //TODO Auto-generated catch block
61 e.printStackTrace();62 }63 try{64 connection.start();65 } catch(JMSException e) {66 //TODO Auto-generated catch block
67 e.printStackTrace();68 }69
70 }71 publicJMSTopic(String user,String name)72 {73 connectionFactory = newActiveMQConnectionFactory(74 user,75 name,76 "tcp://localhost:61616");77 try{78 connection=connectionFactory.createTopicConnection();79 } catch(JMSException e) {80 //TODO Auto-generated catch block
81 e.printStackTrace();82 }83 try{84 connection.start();85 } catch(JMSException e) {86 //TODO Auto-generated catch block
87 e.printStackTrace();88 }89
90 }91
92
93
94
95 //设计session类型
96 public void setSession() throwsJMSException97 {98 session= connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);99
100 }101 //设置为原子类型
102 public void setAtomicSession() throwsJMSException103 {104 session= connection.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);105
106 }107
108 //此处先固定消息为String类型
109 public void writeMessage(String t,String message,intpriority )110 {111 try{112
113 producer=session.createProducer(session.createTopic(t));114
115 //使用message构造TextMessage
116 TextMessage text=session.createTextMessage();117 text.setJMSPriority(priority);118 text.setText(message);119 producer.send(text);120
121
122
123
124 } catch(JMSException e) {125 //TODO Auto-generated catch block
126 e.printStackTrace();127 }128 //创建发布会话应该是可以配置的,此处先固定
129
130
131 }132
133 public voidwriteMessage(String t,Object o)134 {135 try{136
137
138 producer=session.createProducer(session.createTopic(t));139
140 //使用message构造TextMessage
141 ObjectMessage text=session.createObjectMessage();142 text.setObject((Serializable) o);143 producer.send(text);144
145
146
147
148 } catch(JMSException e) {149 //TODO Auto-generated catch block
150 e.printStackTrace();151 }152 //创建发布会话应该是可以配置的,此处先固定
153
154
155 }156
157 //使用某个Message监听器来监听某个Topic
158 public voidreceiveMsg(String c,MessageListener ml)159 {160 try{161
162 Topic t=session.createTopic(c);163 consumer=session.createConsumer(t);164 //设置过来的监视器
165 consumer.setMessageListener(ml);166
167 } catch(JMSException e) {168 //TODO Auto-generated catch block
169 e.printStackTrace();170 }171
172
173
174
175 }176 publicMessage receiveMsg(String c)177 {178 try{179
180 Topic t=session.createTopic(c);181 consumer=session.createConsumer(t);182 //设置过来的监视器
183 Message message=consumer.receive();184 returnmessage;185
186 } catch(JMSException e) {187 //TODO Auto-generated catch block
188 e.printStackTrace();189 }190 return null;191 }192 //同步接收信息
193
194 public void commit() throwsJMSException195 {196 session.commit();197 }198 public void rollback() throwsJMSException199 {200 session.rollback();201 }202 public void close() throwsJMSException203 {204 if(connection!=null) connection.close();205 if(session!=null) session.close();206 if(producer!=null) session.close();207 if(consumer!=null) consumer.close();208
209
210
211
212 }213
214
215 }