JMS的样例
1、JMS是一個由AS提供的Message服務(wù)。它能接受消息產(chǎn)生者(Message Provider)所發(fā)出的消息,并把消息轉(zhuǎn)發(fā)給消息消費者(Message? Consumer)。
2、JMS提供2種類型的消息服務(wù):(1)Queue,即點對點,每一個消息僅僅轉(zhuǎn)發(fā)給一個消息消費者使用。(2)Topic,即公布和訂閱,每一個消息能夠轉(zhuǎn)發(fā)給全部的訂閱者(消費者)。
3、WEBLOGIC 8下的JMS配置:
(1)配置JMS Connection Factory
(2)配置JMS File Store(眼下所找到的文檔都是配置File Store,事實上在詳細的應(yīng)用中,可能JMS JDBC Store更廣泛,但臨時沒有找到資料)
(3)配置JMS Server
(4)在JMS Server的destinations中配置JMS Queue或者JMS Topic
當中提供給消息產(chǎn)生者和消息消費者使用的是JMS Connection Factory的JNDI和JMS Queue或者JMS Topic的JNDI。
4、消息產(chǎn)生者向JMS發(fā)送消息的步驟:
(1)使用JNDI查詢對象JMS ConnectionFactory和Destination(JMS Queue/Topic)
(2)使用管理對象JMS ConnectionFactory建立連接Connection
(3)使用連接Connection 建立會話Session
(4)使用會話Session和管理對象Destination創(chuàng)建消息生產(chǎn)者MessageSender
(5)使用消息生產(chǎn)者MessageSender發(fā)送消息
一個消息發(fā)送者的樣例:
package myjms; import java.util.*; import javax.naming.*; import javax.jms.*; public class MessageProducter { public static void main(String[] args) { String queueConnectionFactoryName = "myjmsconnectionfactory"; //JMS Connection Factory的JNDI String queueName = "myjmsqueue"; //JMS Queue或者JMS Topic的JNDI boolean transacted = false;//transaction模式 int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;//acknowledgement模式 String message="Message need to send";//模擬須要發(fā)送的消息 Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, "t3://localhost:7001"); try { Context context = new InitialContext(properties); Object obj = context.lookup(queueConnectionFactoryName); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;//JMS Connection Factory的獲得 obj = context.lookup(queueName); Queue queue = (Queue) obj;//JMS Queue或者JMS Topic的獲得 QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();//產(chǎn)生連接 queueConnection.start(); QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode); TextMessage textMessage = queueSession.createTextMessage(); textMessage.clearBody(); textMessage.setText(message); QueueSender queueSender = queueSession.createSender(queue); queueSender.send(textMessage); if (transacted) { queueSession.commit(); } if (queueSender != null) { queueSender.close(); } if (queueSession != null) { queueSession.close(); } if (queueConnection != null) { queueConnection.close(); } } catch(Exception ex){ ex.printStackTrace(); } } }
5、消息消費者從JMS接受消息的步驟:
(1)使用JNDI查詢對象JMS ConnectionFactory和Destination(JMS Queue/Topic)
(2)使用管理對象JMS ConnectionFactory建立連接Connection
(3)使用連接Connection 建立會話Session
(4)使用會話Session和管理對象Destination創(chuàng)建消息消費者MessageReceiver
(5)使用消息消費者MessageReceiver接受消息,須要用setMessageListener將MessageListener接口綁定到MessageReceiver
消息消費者必須實現(xiàn)了MessageListener接口,須要定義onMessage事件方法。
一個消息消費者的樣例:
package myjms; import java.util.*; import javax.naming.*; import javax.jms.*; public class MessageReciever implements MessageListener { public void onMessage(Message message) { if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; try { System.out.println("Message content is:" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } } public static void main(String[] args) { MessageReciever msgRcvr=new MessageReciever(); String queueConnectionFactoryName = "myjmsconnectionfactory"; String queueName = "myjmsqueue"; boolean transacted = false; int acknowledgementMode = Session.AUTO_ACKNOWLEDGE; Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, "t3://localhost:7001"); try { Context context = new InitialContext(properties); Object obj = context.lookup(queueConnectionFactoryName); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj; obj = context.lookup(queueName); Queue queue = (Queue) obj; QueueConnection queueConnection = queueConnectionFactory. createQueueConnection(); queueConnection.start(); QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode); QueueReceiver queueReceiver = queueSession.createReceiver(queue); queueReceiver.setMessageListener(msgRcvr); synchronized(msgRcvr){ msgRcvr.wait(100000); } if (queueReceiver != null) { queueReceiver.close(); } if (queueSession != null) { queueSession.close(); } if (queueConnection != null) { queueConnection.close(); } } catch (Exception ex) { ex.printStackTrace(); } } }
6、Message-driven Bean
MDB實際上就是一個消息消費者的client程序。它由AS EJB Container來管理。在JBUILDER生成一個MDB很easy。
轉(zhuǎn)載于:https://www.cnblogs.com/zfyouxi/p/4470873.html
總結(jié)
- 上一篇: START
- 下一篇: div设置宽度,实现不等比缩放,或设置最