在上一篇文章中,介紹瞭如何在weblogic中建立jms相關資源,下面要介紹如何經過java向jms隊列中寫入消息以及如何從jms隊列中取出消息。
要使用weblogic的jms,須要引入如下兩個包java
javax.jms.jar
wlfullclient.jar
若是是使用jdeveloper開發,直接引入如下兩個Library便可web
java將消息發送到消息隊列中,須要通過如下步驟segmentfault
具體代碼實現:bash
package asan.demo.jms; import java.util.Hashtable; import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class JMSSender { private QueueSender sender = null; private QueueSession session = null; private static final String JMS_FACTORY_JNDI = "jms/jms_test_connection_factory1"; private static final String JMS_QUEUE_JNDI = "jms/jms_test_queue"; public JMSSender() { 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. 經過鏈接工廠建立隊列鏈接(QueueConnection) // 4. 經過隊列鏈接建立隊列會話(QueueSession) // 5. 經過隊列會話建立隊列生產者(Sender/Product) // 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); QueueConnectionFactory jmsFactory = (QueueConnectionFactory)ctx.lookup(JMS_FACTORY_JNDI); QueueConnection jmsConn = jmsFactory.createQueueConnection(); session = jmsConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = (Queue)ctx.lookup(JMS_QUEUE_JNDI); sender = session.createSender(queue); } public static void main(String[]cmd){ JMSSender sender=new JMSSender(); sender.sendMessage("hello world"); } }
運行程序後登陸console,進入domain->Services->Messaging->JMS Module->jms_test_module->jms_test_queue
在Monitoring頁面能夠看到隊列中增長一條消息,點擊Show Messages
能夠查看消息詳細內容服務器
java從消息隊列中獲取消息,須要通過如下步驟session
和消息發送到步驟差很少。
具體代碼實現:dom
package asan.demo.jms; import java.util.Hashtable; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class JMSReciver { private MessageConsumer reciver = null; private static final String JMS_FACTORY_JNDI = "jms/jms_test_connection_factory1"; private static final String JMS_QUEUE_JNDI = "jms/jms_test_queue"; public JMSReciver() { super(); } public void reciveMessage() { try { if (this.reciver == null) { this.init(); } System.out.println("waiting to recive message from jms queue "+JMS_QUEUE_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. 經過鏈接工廠建立隊列鏈接(QueueConnection) // 4. 經過隊列鏈接建立隊列會話(QueueSession) // 5. 經過隊列會話建立隊列消費者(Reciver/Consumer) // 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); QueueConnectionFactory jmsFactory = (QueueConnectionFactory)ctx.lookup(JMS_FACTORY_JNDI); QueueConnection jmsConn = jmsFactory.createQueueConnection(); QueueSession session = jmsConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = (Queue)ctx.lookup(JMS_QUEUE_JNDI); reciver = session.createReceiver(queue); jmsConn.start(); } public static void main(String[]cmd){ JMSReciver consumer=new JMSReciver(); consumer.reciveMessage(); } }
運行程序,控制檯打印出隊列中消息this
爲了更清楚瞭解jms消息發送過程,這邊寫了一個客戶端,該客戶端有兩種模式,看成爲生產者能夠在控制檯輸入消息發送到消息隊列中,看成爲消費者,一旦消息隊列中有消息產生,馬上將消息打印到控制檯。
客戶端代碼以下:spa
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"); System.out.println("sender:向jms隊列發送消息"); System.out.println("reciver:從隊列中取出消息"); } 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); System.out.println("input you message(input end to exit):"); while (true) { String msg = sc.nextLine(); if ("end".equalsIgnoreCase(msg)) { return; } sender.sendMessage(msg); } } else { JMSReciver consumer = new JMSReciver(); consumer.reciveMessage(); } } }
將代碼打包(關於如何打包參考這篇文章),jar名稱爲JMSClient.jar
,執行如下命令將客戶端做爲生產者運行code
java -jar JMSClient.jar sender
從新打開一個控制檯,執行如下命令將客戶端做爲消費者運行
java -jar JMSClient.jar reciver
在第一個控制檯中輸入消息,消息立馬在第二個控制檯輸出
程序稍加改造就能變成一個實時點對點聊天程序,思路是在weblogic中建立兩個隊列每一個客戶端對應一個隊列,兩個客戶端分別向對方隊列發送消息並從本身的隊列中獲取消息。