当前位置: 代码迷 >> 综合 >> 【Java.JMS】发布/订阅通信——Topic
  详细解决方案

【Java.JMS】发布/订阅通信——Topic

热度:6   发布时间:2023-12-14 19:44:20.0

一对多发布主题/接收——对应的数据通道是Topic  , 消息生产者TopicPublisher和消息消费者TopicSubscriber

javax.jms
public interface TopicPublisher extends MessageProducer
public interface TopicSubscriber extends MessageConsumer


和Topic相关的接口还有:

javax.jms
public interface TopicConnectionFactory extends ConnectionFactory
public interface TopicConnection extends Connection
public interface TopicSession extends Session


( Session和Connection在使用之后需要关闭。

在消息接收前需要打开Connection,connection.start();)


发布/订阅通信模型如下:



发布/订阅JMS应用程序的编程模型如下:



实例:

我们通过Active MQ Admin Portlet手动去Send消息到Topic中,在代码中创建两个Consumer去异步接收这个消息。
消费者代码如下:

package com.gof.jms.test;import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class TopicMessageApp {public static final String user = "system";public static final String password = "manager";public static final String url = "tcp://localhost:61616";public static final String topicName = "test_topic";public static final boolean transacted = false;public static final boolean persistent = false;public static void main(String[] args){Connection connection = null;Session session = null;try{// create the connectionConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);connection = connectionFactory.createConnection();connection.start();// create the sessionsession = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);Destination destination = session.createTopic(topicName);// In this example, we will send the message through activemq admin portlet manually.// create the consumer1 MessageConsumer consumer1 = session.createConsumer(destination);consumer1.setMessageListener(new MessageListener() {public void onMessage(Message message) {// TODO Auto-generated method stubTextMessage recvMessage = (TextMessage)message;try{System.out.println("Receive message by the 1st Consumer: " + recvMessage.getText());}catch (JMSException e){e.printStackTrace();}}});// create the consumer2 MessageConsumer consumer2 = session.createConsumer(destination);consumer2.setMessageListener(new MessageListener() {public void onMessage(Message message) {// TODO Auto-generated method stubTextMessage recvMessage = (TextMessage)message;try{System.out.println("Receive message by the 2nd Consumer: " + recvMessage.getText());}catch (JMSException e){e.printStackTrace();}}});// To avoid the connection closed before the message listener received the message.Thread.sleep(5000000);}catch (Exception e){e.printStackTrace();}finally{try{// close session and connectionif (session != null){session.close();}if (connection != null){connection.close();}}catch (Exception e){e.printStackTrace();}}}
}<span style="font-family:SimSun;">
</span>


首先启动Active MQ,再启动上面的消费者程序。

在Active MQ Admin Porlet中手动发送Topic消息到目标Topic(在Active MQ中先创建上面的Topic),



可以得到如下的程序输出:



说明每一个消息都会被所有的消费者所消费




  相关解决方案