一个Topic消息的发送与接收实例。通过一个实例来进一步了解Pub/Sub模型
Topic消息允许多个主题订阅者接收同一条消息。
MyTopicMDBBean.java——Topic消息接收者一
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.jinbo.cn; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; //通过注解配置 MDB 是一个什么样的消费者. 这里p2p消费者 @MessageDriven( activationConfig={ @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/mytopic") } ) public class MyTopicMDBBean implements MessageListener { public void onMessage(Message msg) { //转换为 TextMessage TextMessage message = (TextMessage)msg; try { System.out.println("MyTopicMDBBean被调用了:[" + message.getText() +"]"); } catch (JMSException e) { e.printStackTrace(); } } }</span>
MyTopicMDBBean02.java——Topic消息接收者二
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.jinbo.cn; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; //通过注解配置 MDB 是一个什么样的消费者. 这里p2p消费者 @MessageDriven( activationConfig={ @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/mytopic") } ) public class MyTopicMDBBean02 implements MessageListener { public void onMessage(Message msg) { //转换为 TextMessage TextMessage message = (TextMessage)msg; try { System.out.println("MyTopicMDBBean02被调用了:[" + message.getText() +"]"); } catch (JMSException e) { e.printStackTrace(); } } } </span>
MyTopicMDBBeanClient.java——Topic消息发送者
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.jinb.cn; 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.InitialContext; public class MyTopicMDBBeanClient { public static void main(String[] args) throws Exception { InitialContext context = new InitialContext(); //获得QueueConnectionFactory对象 TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup("ConnectionFactory"); //创建QueueConnection TopicConnection connection = (TopicConnection)factory.createConnection(); /** * 通过connection创建TopicSession对象; * 其中第一个参数为是否支持事务,TRUE为支持,false为不支持; * 若设为true,则需要手动COMMIT; * 第二个参数为响应的模式,一般情况下就设为TopicSession.AUTO_ACKNOWLEDGE * */ TopicSession session = (TopicSession)connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); //获取Destination对象 Topic topic = (Topic)context.lookup("topic/mytopic"); //创建文本消息 TextMessage message = session.createTextMessage("世界, 你好 ! 我来了."); //创建发布者,发送到Topic 目的对象 TopicPublisher publisher = session.createPublisher(topic); //发布消息 publisher.send(message); //关闭资源 session.close(); connection.close(); System.out.println("消息已经发送!"); } } </span>
运行结果:
MyQueueMDBBean被调用了:[世界,你好!我来了.]
MyQueueMDBBean2被调用了:[世界,你好!我来了.]