Java jms學習
阿新 • • 發佈:2018-11-09
/** * <html> * <body> * <P> https://github.com/Jasonandy </p> * <p> All rights reserved.</p> * <p> Created on 2018年6月12日 下午4:00:58</p> * <p> Created by Jason</p> * </body> * </html> */ package cn.ucaner.component.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; /** * @Package:cn.ucaner.component.jms * @ClassName:MsgPublisher * @Description: <p> jms publiser </p> * <url> https://blog.csdn.net/zhangzikui/article/details/24837999 </url> * @Author: - Jason * @CreatTime:2018年6月12日 下午4:00:58 * @Modify By: * @ModifyTime: 2018年6月12日 * @Modify marker: *@version V1.0 */ public class MsgPublisher { /** * JMS連線,屬於Pub/Sub方式的連線 */ TopicConnection topicConnection; /** * JMS會話,屬於Pub/Sub方式的會話 */ TopicSession topicSession; /** * 訊息釋出者 */ TopicPublisher topicPublisher; /** * 主題 */ Topic topic; public MsgPublisher(String factoryJNDI, String topicJNDI)throws JMSException, NamingException { Hashtable<String, String> env = new Hashtable<String, String>(); /** * 設定好連線JMS容器的屬性,不同的容器需要的屬性可能不同,需要查閱相關文件 */ env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL, "localhost:1088"); env.put("java.naming.rmi.security.manager", "yes"); env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming"); /** * 建立連線JMS容器的上下文(context) */ Context context = new InitialContext(env); /** * 通過連線工廠的JNDI名查詢ConnectionFactory */ TopicConnectionFactory topicFactory = (TopicConnectionFactory) context.lookup(factoryJNDI); /** * 用連線工廠建立一個JMS連線 */ topicConnection = topicFactory.createTopicConnection(); /** * 通過JMS連線建立一個Session */ topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); /** * 通過上下文查詢到一個主題(topic) */ topic = (Topic) context.lookup(topicJNDI); /** * 用session來建立一個特定主題的訊息傳送者 */ topicPublisher = topicSession.createPublisher(topic); } /** * @Description: 釋出一條文字訊息 * @param msg 待發布的訊息 * @throws JMSException void * @Autor: Jason - [email protected] */ public void publish(String msg) throws JMSException { /** * 用session來建立一個文字型別的訊息 */ TextMessage message = topicSession.createTextMessage(); /** * 設定訊息內容 */ message.setText(msg); /** * 訊息傳送,傳送到特定主題 */ topicPublisher.publish(topic, message); } public void close() throws JMSException { topicSession.close();//關閉session topicConnection.close();//關閉連線 } public static void main(String[] args)throws JMSException, NamingException { MsgPublisher publisher = new MsgPublisher("ConnectionFactory", "topic/testTopic"); try { for (int i = 1; i < 11; i++) { String msg = "Hello World no. " + i; System.out.println("Publishing message: " + msg); publisher.publish(msg); } /** * session和connection用完之後一定記得關閉 */ publisher.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
/** * <html> * <body> * <P> https://github.com/Jasonandy </p> * <p> All rights reserved.</p> * <p> Created on 2018年6月12日 下午4:00:58</p> * <p> Created by Jason</p> * </body> * </html> */ package cn.ucaner.component.jms; import java.util.Hashtable; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; 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; /** * @Package:cn.ucaner.component.jms * @ClassName:MsgSubscriber * @Description: <p> MsgSubscriber </p> * <url> https://blog.csdn.net/zhangzikui/article/details/24837999 </url> * @Author: - * @CreatTime:2018年6月12日 下午4:10:55 * @Modify By: * @ModifyTime: 2018年6月12日 * @Modify marker: * @version V1.0 */ public class MsgSubscriber implements MessageListener{ TopicConnection topicConnection; TopicSession topicSession; TopicSubscriber topicSubscriber; Topic topic; @Override public void onMessage(Message msg) { try { if (msg instanceof TextMessage) { //把Message 轉型成 TextMessage 並提取訊息內容 String msgTxt = ((TextMessage) msg).getText(); System.out.println("HelloSubscriber got message: " +msgTxt); } } catch (JMSException ex) { System.err.println("Could not get text message: " + ex); ex.printStackTrace(); } } public MsgSubscriber(String factoryJNDI, String topicJNDI)throws JMSException, NamingException { Hashtable<String, String> env = new Hashtable<String, String>(); //設定好連線JMS容器的屬性,不同的容器需要的屬性可能不同,需要查閱相關文件 env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL, "localhost:1088"); env.put("java.naming.rmi.security.manager", "yes"); env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming"); Context context = new InitialContext(); TopicConnectionFactory topicFactory = (TopicConnectionFactory) context.lookup(factoryJNDI); //建立連線 topicConnection = topicFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);//建立session topic = (Topic) context.lookup(topicJNDI);//查詢到主題 //用session建立一個特定queue的訊息接收者 topicSubscriber = topicSession.createSubscriber(topic); //註冊監聽,這裡設定的監聽是自己,因為本類已經實現了MessageListener介面, //一旦queueReceiver接收到了訊息,就會呼叫本類的onMessage方法 topicSubscriber.setMessageListener(this); System.out.println("HelloSubscriber subscribed to topic: "+ topicJNDI); topicConnection.start();//啟動連線,這時監聽器才真正生效 } public void close() throws JMSException { topicSession.close(); topicConnection.close(); } public static void main(String[] args) { try { new MsgSubscriber("TopicConnectionFactory","topic/testTopic"); } catch (Exception ex) { ex.printStackTrace(); } } }