在上一篇介紹如何經過java往jms消息隊列裏面寫消息和讀取消息,本文介紹如何經過java往jms主題裏寫消息和讀取消息。java
一樣將消息發佈到主題中,須要通過如下步驟web
代碼實現:segmentfault
package asan.demo.jms; import java.util.Hashtable; import javax.jms.JMSException; import javax.jms.Session; import javax.jms.TextMessage; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicPublisher; import javax.jms.TopicSession; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class JMSTopicSender { private TopicPublisher sender = null; private TopicSession session = null; private static final String JMS_FACTORY_JNDI = "jms/jms_test_connection_factory1"; private static final String JMS_TOPIC_JNDI = "jms/jms_test_topic"; public JMSTopicSender() { super(); } public void sendMessage(String msg) { TextMessage textMsg; try { if (this.sender == null) { this.init(); } textMsg = session.createTextMessage(); textMsg.setText(msg); sender.send(textMsg); } catch (JMSException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } // 1. 鏈接jms服務器 // 2. 獲取鏈接工廠(Connection Factory) // 3. 經過鏈接工廠建立主題鏈接(TopicConnection) // 4. 經過主題鏈接建立主題會話(TopicSession) // 5. 經過主題會話建立主題發佈者(Publisher) // 6. 建立消息(Message) // 7. 經過發佈者將消息發送到主題中 private void init() throws NamingException, JMSException { Hashtable properties = new Hashtable(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101"); properties.put(Context.SECURITY_PRINCIPAL, "weblogic"); properties.put(Context.SECURITY_CREDENTIALS, "weblogic1"); InitialContext ctx = new InitialContext(properties); TopicConnectionFactory jmsFactory = (TopicConnectionFactory)ctx.lookup(JMS_FACTORY_JNDI); TopicConnection jmsConn = jmsFactory.createTopicConnection(); session = jmsConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic)ctx.lookup(JMS_TOPIC_JNDI); sender = session.createPublisher(topic); } public static void main(String[]cmd){ JMSTopicSender sender=new JMSTopicSender(); sender.sendMessage("hello jms topic"); } }
與隊列不一樣的是,此時主題沒有訂閱者,那麼該消息就不存儲在主題中,即便後面有訂閱者訂閱了該主題,也沒法接收訂閱前的消息。bash
從主題中訂閱消息,須要通過如下步驟:服務器
代碼實現:session
package asan.demo.jms; import java.util.Hashtable; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicSession; import javax.jms.TopicSubscriber; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class JMSTopicReciver { private TopicSubscriber reciver = null; private static final String JMS_FACTORY_JNDI = "jms/jms_test_connection_factory1"; private static final String JMS_TOPIC_JNDI = "jms/jms_test_topic"; public JMSTopicReciver() { super(); } public void reciveMessage() { try { if (this.reciver == null) { this.init(); } System.out.println("waiting to recive message from jms topic "+JMS_TOPIC_JNDI); while(true){ Message msg=reciver.receive(); if(msg instanceof TextMessage){ TextMessage textMsg=(TextMessage)msg; System.out.println("recive jms message:"+textMsg.getText()); } } } catch (JMSException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } // 1. 鏈接jms服務器 // 2. 獲取鏈接工廠(Connection Factory) // 3. 經過鏈接工廠建立主題鏈接(TopicConnection) // 4. 經過主題鏈接建立主題會話(TopicSession) // 5. 經過主題會話建立訂閱者(Subscriber) // 6. 接收消息(Message) private void init() throws NamingException, JMSException { Hashtable properties = new Hashtable(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101"); properties.put(Context.SECURITY_PRINCIPAL, "weblogic"); properties.put(Context.SECURITY_CREDENTIALS, "weblogic1"); InitialContext ctx = new InitialContext(properties); TopicConnectionFactory jmsFactory = (TopicConnectionFactory)ctx.lookup(JMS_FACTORY_JNDI); TopicConnection jmsConn = jmsFactory.createTopicConnection(); TopicSession session = jmsConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic)ctx.lookup(JMS_TOPIC_JNDI); reciver = session.createSubscriber(topic); jmsConn.start(); } public static void main(String[]cmd){ JMSTopicReciver consumer=new JMSTopicReciver(); consumer.reciveMessage(); } }
運行代碼,此時運行以前消息發佈的代碼,能夠在控制檯看到發送的消息dom
一樣,稍微修改下上一篇文章的客戶端程序,使整個過程看起來更清晰,修改後的客戶端代碼以下:this
package asan.demo.jms; import java.util.Scanner; public class JMSClient { public JMSClient() { super(); } public static void help() { System.out.println("Usage:java -jar JMSClient.jar sender/reciver/topicSender/topicReciver"); System.out.println("sender:向jms隊列發送消息"); System.out.println("reciver:從隊列中取出消息"); System.out.println("topicSender:向jms主題發送消息"); System.out.println("topicReciver:從主題中取出消息"); } public static void main(String[] cmd) { if (cmd.length == 0) { help(); return; } String mode = cmd[0]; if ("sender".equalsIgnoreCase(mode)) { JMSSender sender = new JMSSender(); Scanner sc = new Scanner(System.in); while (true) { System.out.println("input you message(input end to exist):"); String msg = sc.nextLine(); if ("end".equalsIgnoreCase(msg)) { return; } sender.sendMessage(msg); System.out.println("message send success"); } } else if ("reciver".equalsIgnoreCase(mode)) { JMSReciver consumer = new JMSReciver(); consumer.reciveMessage(); } else if ("topicSender".equalsIgnoreCase(mode)) { JMSTopicSender sender = new JMSTopicSender(); Scanner sc = new Scanner(System.in); while (true) { System.out.println("input you message(input end to exist):"); String msg = sc.nextLine(); if ("end".equalsIgnoreCase(msg)) { return; } sender.sendMessage(msg); System.out.println("message send success"); } } else if ("topicReciver".equalsIgnoreCase(mode)) { JMSTopicReciver consumer = new JMSTopicReciver(); consumer.reciveMessage(); } } }
打包運行,執行如下命令將客戶端做爲主題發佈者spa
java -jar JMSDemo.jar topicSender
新建窗口,執行如下命令將客戶端做爲主題訂閱者code
java -jar JMSDemo.jar topicReciver
在發佈者上發送消息,在訂閱者上看到收到的消息
能夠再多開一個窗口,再運行一個訂閱者
登陸weblogic控制檯,進入domain->Services->Messaging->JMS Modules->jms_test_module->jms_test_topic
能夠查看當前主題訂閱統計信息