JMS Learning Seven (Durable Subscription of ActiveMQ Topic)

Non-durable subscriptions last for the lifetime of the objects they subscribe to. This means that clients can only see messages published by related topics when subscribers are active. If a subscriber is inactive, it will miss messages on related topics. Subscribers can be defined as durable if it costs a lot of money. A durable subscriber registers a durable subscription with a unique identifier maintained by JMS. Subsequent subscribers with the same ID will renew the subscription status of the previous subscriber. If the durable subscription has no active subscribers, JMS will keep the subscription message until the message is received by the subscription or expires.

Producer:

package cn.slimsmart.activemq.demo.topic;  
  
import javax.jms.Connection;  
import javax.jms.DeliveryMode;  
import javax.jms.JMSException;  
import javax.jms.MessageProducer;  
import javax.jms.Session;  
import javax.jms.TextMessage;  
import javax.jms.Topic;  
  
import org.apache.activemq.ActiveMQConnectionFactory;  
  
public class Producer {  
  
    public  static  void main(String[] args) throws JMSException {  
         // Connect to ActiveMQ server   
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.18.43:61616" );  
        Connection connection = factory.createConnection();  
        connection.start();  
        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);  
        // 创建主题  
        Topic topic = session.createTopic("slimsmart.topic.test");  
        MessageProducer producer = session.createProducer(topic);  
         // NON_PERSISTENT non-persistent PERSISTENT persistent, use persistent mode when sending messages   
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
        TextMessage message = session.createTextMessage();  
        message.setText("topic 消息。");  
        message.setStringProperty( "property", "Message Property" );  
         // Publish topic message   
        producer.send(message);  
        System.out.println("Sent message: " + message.getText());  
        session.commit();  
        session.close();  
        connection.close();  
    }  
}  

 

consumer:

package cn.slimsmart.activemq.demo.topic;  
  
import javax.jms.Connection;  
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.MessageConsumer;  
import javax.jms.MessageListener;  
import javax.jms.Session;  
import javax.jms.TextMessage;  
import javax.jms.Topic;  
  
import org.apache.activemq.ActiveMQConnectionFactory;  
  
/** 
 * Durable subscription sets unique client ID and subscriber ID.
 */   
public  class ConsumerPersistent {  
  
    public static void main(String[] args) throws JMSException {  
        String clientId = "client_id";  
          
        // Connect to ActiveMQ server   
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.18.43:61616" );  
        Connection connection = factory.createConnection();  
         // Client ID, persistent subscription needs to be set   
        connection.setClientID(clientId);  
        connection.start();  
        Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);  
         // Create a topic   
        Topic topic = session.createTopic("slimsmart.topic.test" );  
         // Create a durable subscription and specify the client ID.  
        MessageConsumer consumer = session.createDurableSubscriber(topic,clientId);  
        consumer.setMessageListener(new MessageListener() {  
            // 订阅接收方法  
            public void onMessage(Message message) {  
                TextMessage tm = (TextMessage) message;  
                try {  
                    System.out.println("Received message: " + tm.getText()+":"+tm.getStringProperty("property"));  
                } catch (JMSException e) {  
                    e.printStackTrace ();  
                }  
            }  
        });  
  
    }  
}  

 

Note:

 

1. Activemq distinguishes consumers by clientID and subscription customer name.

2. The producer of the message uses the persistent mode when sending the message.
3. If the same "clientID" is used, it is considered to be the same consumer. If two programs use the same "clientID", only one of them can be connected to activemq at the same time, and the second connection will report an error.
4. The setting of activemq is in conf/activemq.xml, the default message is saved in data/kahadb, and the message will not be lost when restarting activemq.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325103617&siteId=291194637