activeMQ笔记(三)——Topic消息

来源:互联网 发布:el表达式获取js变量 编辑:程序博客网 时间:2024/06/05 15:34

1、JMS Pub/Sub 模型定义了如何向一个内容节点发布和订阅消息,这些节点被称作topic 

主题可以被认为是消息的传输中介,发布者(publisher)发布消息到主题,订阅者 (subscribe) 从主题订阅消息。主题使得消息订阅者和消息发布者保持互相独立,不需要 接触即可保证消息的传送。

2、Pub/Sub的一些特点:

  2.1、消息订阅分为非持久订阅和持久订阅 

2.1.1非持久订阅只有当客户端处于激活状态,也就是和JMS Provider保持连接状态才能 收到发送到某个主题的消息,而当客户端处于离线状态,这个时间段发到主题的消息将会 丢失,永远不会收到

2.1.2持久订阅时,客户端向JMS 注册一个识别自己身份的ID,当这个客户端处于离线 时,JMS Provider会为这个ID 保存所有发送到主题的消息,当客户再次连接到JMS Provider时,会根据自己的ID 得到所有当自己处于离线时发送到主题的消息

  2.2、如果用户在receive 方法中设定了消息选择条件,那么不符合条件的消息不会被接收

  2.3、非持久订阅状态下,不能恢复或重新派送一个未签收的消息。只有持久订阅才能恢复或重 新派送一个未签收的消息。 

2.4、当所有的消息必须被接收,则用持久订阅。当丢失消息能够被容忍,则用非持久订阅

3、topic实例编码

3.1、非持久化topic

3.1.1、生产者

package net.tudoutiao.topic;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * Created by rc on 2017/2/14. */public class NoPersistTopicProducer {    private Session session;    public static void main(String[] args) throws Exception {        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.109.128:61616");        Connection connection = connectionFactory.createConnection();        connection.start();        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);        Topic topic = session.createTopic("tudoutiao");        MessageProducer producer = session.createProducer(topic);        for (int i = 0; i < 3; i++) {            TextMessage textMessage = session.createTextMessage("todoutiaoTopic--" + i);            producer.send(textMessage);        }        session.commit();        session.close();        connection.close();    }}


3.1.2、消费者

package net.tudoutiao.topic;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;/** * Created by rc on 2017/2/14. */public class NoPersistTopicConsumer {    public static void main(String[] args) throws Exception {        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.109.128:61616");        Connection connection = connectionFactory.createConnection();        connection.start();        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);        Topic topic = session.createTopic("tudoutiao");        MessageConsumer consumer = session.createConsumer(topic);        Message message = consumer.receive();        while (null != message) {            TextMessage textMessage = (TextMessage) message;            System.out.println("topic消息为:" + textMessage.getText());            message = consumer.receive(1000L);        }        session.commit();        session.close();        connection.close();    }}


3.2、持久化topic


3.1.1、生产者

public class PersistTopicProducer {    public static void main(String[] args) throws Exception{        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.109.128:61616");        Connection connection = connectionFactory.createConnection();        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);        Topic topic = session.createTopic("fanqiejiang");        MessageProducer producer = session.createProducer(topic);        //持久化订阅,发送消息者要用 DeliveryMode.PERSISTENT 模式发现,在连接之前设定        //使用setDeliveryMode 方法设置消息的投递模式,那么所有的消息都采用此传送模式        producer.setDeliveryMode(DeliveryMode.PERSISTENT);        connection.start();        for (int i=0;i<5;i++){            TextMessage textMessage = session.createTextMessage("fanqiejiang----" + i);            //public void send(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException  使用send 方法为每一条消息设置传送模式           producer.send(textMessage);        }        session.commit();        session.close();        connection.close();    }}


3.1.2、消费者

public class PersistTopicConsumer {    public static void main(String[] args)throws Exception {        ConnectionFactory connectionFactory =                new ActiveMQConnectionFactory("tcp://192.168.109.128:61616");        Connection connection = connectionFactory.createConnection();        //要在连接上设置消费者id,用来识别消费者        connection.setClientID("TDT");        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);        Topic topic = session.createTopic("fanqiejiang");        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "tdt1");        connection.start();        Message message = topicSubscriber.receive();        while (null != message){            TextMessage textMessage = (TextMessage)message;            session.commit();            System.out.println("收到来自土豆条的消息:"+textMessage.getText());            message = topicSubscriber.receive();        }        session.close();        connection.close();    }}


0 0