JMS的例子

来源:互联网 发布:陈一发 整容 知乎 编辑:程序博客网 时间:2024/05/18 09:10

原文:http://blog.csdn.net/yutel/article/details/3874439 



1、JMS是一个由AS提供的Message服务。它能接受消息产生者(Message Provider)所发出的消息,并把消息转发给消息消费者(Message  Consumer)。
2、JMS提供2种类型的消息服务:(1)Queue,即点对点,每个消息只转发给一个消息消费者使用。(2)Topic,即发布和订阅,每个消息可以转发给所有的订阅者(消费者)。
3、WEBLOGIC 8下的JMS配置:
(1)配置JMS Connection Factory
(2)配置JMS File Store(目前所找到的文档都是配置File Store,其实在具体的应用中,可能JMS JDBC Store更广泛,但暂时没有找到资料)
(3)配置JMS Server
(4)在JMS Server的destinations中配置JMS Queue或者JMS Topic
其中提供给消息产生者和消息消费者使用的是JMS Connection Factory的JNDI和JMS Queue或者JMS Topic的JNDI。
4、消息产生者向JMS发送消息的步骤:
(1)使用JNDI查询对象JMS ConnectionFactory和Destination(JMS Queue/Topic)
(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息生产者MessageSender
(5)使用消息生产者MessageSender发送消息
一个消息发送者的例子:

[java] view plaincopy
  1. package myjms;  
  2. import java.util.*;  
  3. import javax.naming.*;  
  4. import javax.jms.*;  
  5. public class MessageProducter {  
  6.   public static void main(String[] args) {  
  7.     String queueConnectionFactoryName = "myjmsconnectionfactory"//JMS Connection Factory的JNDI  
  8.     String queueName = "myjmsqueue"//JMS Queue或者JMS Topic的JNDI  
  9.     boolean transacted = false;//transaction模式  
  10.     int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;//acknowledgement模式  
  11.     String message="Message need to send";//模拟需要发送的消息  
  12.     Properties properties = new Properties();  
  13.     properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");  
  14.     properties.put(Context.PROVIDER_URL, "t3://localhost:7001");  
  15.     try {  
  16.       Context context = new InitialContext(properties);  
  17.       Object obj = context.lookup(queueConnectionFactoryName);  
  18.       QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;//JMS Connection Factory的获得  
  19.        
  20.       obj = context.lookup(queueName);  
  21.       Queue queue = (Queue) obj;//JMS Queue或者JMS Topic的获得  
  22.       QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();//产生连接  
  23.       queueConnection.start();  
  24.       QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);  
  25.       TextMessage textMessage = queueSession.createTextMessage();  
  26.       textMessage.clearBody();  
  27.       textMessage.setText(message);  
  28.       QueueSender queueSender = queueSession.createSender(queue);  
  29.       queueSender.send(textMessage);  
  30.       if (transacted) {  
  31.         queueSession.commit();  
  32.       }  
  33.       if (queueSender != null) {  
  34.         queueSender.close();  
  35.       }  
  36.       if (queueSession != null) {  
  37.         queueSession.close();  
  38.       }  
  39.       if (queueConnection != null) {  
  40.         queueConnection.close();  
  41.       }  
  42.     }  
  43.     catch(Exception ex){  
  44.       ex.printStackTrace();  
  45.     }  
  46.   }  
  47. }  

5、消息消费者从JMS接受消息的步骤:
(1)使用JNDI查询对象JMS ConnectionFactory和Destination(JMS Queue/Topic)
(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息消费者MessageReceiver
(5)使用消息消费者MessageReceiver接受消息,需要用setMessageListener将MessageListener接口绑定到MessageReceiver
消息消费者必须实现了MessageListener接口,需要定义onMessage事件方法。
一个消息消费者的例子:

[java] view plaincopy
  1. package myjms;  
  2. import java.util.*;  
  3. import javax.naming.*;  
  4. import javax.jms.*;  
  5. public class MessageReciever  
  6.     implements MessageListener {  
  7.   public void onMessage(Message message) {  
  8.     if (message instanceof TextMessage) {  
  9.       TextMessage textMessage = (TextMessage) message;  
  10.       try {  
  11.         System.out.println("Message content is:" + textMessage.getText());  
  12.       }  
  13.       catch (JMSException e) {  
  14.         e.printStackTrace();  
  15.       }  
  16.     }  
  17.   }  
  18.   public static void main(String[] args) {  
  19.      
  20.     MessageReciever msgRcvr=new MessageReciever();  
  21.     String queueConnectionFactoryName = "myjmsconnectionfactory";  
  22.     String queueName = "myjmsqueue";  
  23.     boolean transacted = false;  
  24.     int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;  
  25.     Properties properties = new Properties();  
  26.     properties.put(Context.INITIAL_CONTEXT_FACTORY,  
  27.                    "weblogic.jndi.WLInitialContextFactory");  
  28.     properties.put(Context.PROVIDER_URL, "t3://localhost:7001");  
  29.     try {  
  30.       Context context = new InitialContext(properties);  
  31.       Object obj = context.lookup(queueConnectionFactoryName);  
  32.       QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)  
  33.           obj;  
  34.       obj = context.lookup(queueName);  
  35.       Queue queue = (Queue) obj;  
  36.       QueueConnection queueConnection = queueConnectionFactory.  
  37.           createQueueConnection();  
  38.       queueConnection.start();  
  39.       QueueSession queueSession = queueConnection.createQueueSession(transacted,  
  40.           acknowledgementMode);  
  41.       QueueReceiver queueReceiver = queueSession.createReceiver(queue);  
  42.       queueReceiver.setMessageListener(msgRcvr);  
  43.       synchronized(msgRcvr){  
  44.         msgRcvr.wait(100000);  
  45.       }  
  46.       if (queueReceiver != null) {  
  47.         queueReceiver.close();  
  48.       }  
  49.       if (queueSession != null) {  
  50.         queueSession.close();  
  51.       }  
  52.       if (queueConnection != null) {  
  53.         queueConnection.close();  
  54.       }  
  55.     }  
  56.     catch (Exception ex) {  
  57.       ex.printStackTrace();  
  58.     }  
  59.   }  
  60. }  

6、Message-driven Bean
MDB实际上就是一个消息消费者的客户端程序。它由AS EJB Container来管理。在JBUILDER生成一个MDB非常简单。


0 0
原创粉丝点击