ActiveMQ----->How to ensure message reliability

Endurance

1.1 API settings

java默认是持久化的

 

1.2 Topic persistence

默认In this case, when the topic is published, if the subscriber is not online.消息就会丢失

But we can use persistent Topic to solve this problem.

先启动订阅在启动生产

Producer

public class TopicProducer {
    public static final String ACTIVE_URL = "tcp://192.168.6.10:61616";
    public static final String TOPIC_NAME = "topic01";
 
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVE_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
 
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
 
        MessageProducer producer = session.createProducer(topic);
        //与之前的实现没有区别,只是将消息设置为持久化后再开始连接即可
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();
        for (int i = 0; i < 3; i++) {
            TextMessage textMessage = session.createTextMessage("msg---" + i);
            producer.send(textMessage);
        }
 
        producer.close();
        session.close();
        connection.close();
        System.out.println("发送持久化topic完成~");
 
    }
}

subscriber

public class TopicConsumer {
    public static final String ACTIVE_URL = "tcp://192.168.6.10:61616";
    public static final String TOPIC_NAME = "topic01";
    
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVE_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        //要设置一个订阅该主题的订阅者Id
        connection.setClientID("ww");
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
        //这里与之前不同,要通过session创建一个持久化的订阅者
        //参数为订阅的主题与备注
        TopicSubscriber t = session.createDurableSubscriber(topic, "我是ww");
        connection.start();
        //阻塞式接收消息
        Message message = t.receive();
 
        while(message != null){
            TextMessage textMessage = (TextMessage) message;
            System.out.println("收到的持久化topic :"+textMessage.getText());
            message = t.receive();
        }
 
 
    }
 
}

Two affairs

Transaction producer

2.1 Producer Affairs

The configuration priority of the transaction is higher than the response mode (signature)

		//第一个参数是是否创建session,第二个参数表示应答模式
        Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);

When the transaction is not opened,
as long as send is executed, it enters the queue

After opening the transaction, it
needs to be used at the end session.commit();, and an exception can also be usedsession.rollback();

2.2 Consumer Affairs

When the transaction is not opened, the
message is considered as consumed

When the transaction is not opened , the message will not be counted as being consumed. It might be possible
commit重复消费的

 

The significance of transaction opening is that if multiple messages must be transmitted in the same batch, transactions can be used. If one transmission fails, the transaction can be rolled back and transmitted again to ensure data integrity .

1) For producers

The message producer focuses on transactions. After the transaction is opened, the send message will only be submitted to the server after the session executes commit, otherwise it will be invalid.

2) For consumers

Consumers prefer to sign , but after the transaction is opened, whether the message is consumed is only related to whether the message is committed or not. The message will be automatically signed after commit. If it is rolled back, the message will continue to be transmitted. If the transaction is not opened, it needs to be passed. Sign for message consumption (mentioned below)
 

 

Three sign for

Sign for partial consumers

3.1 Non-transactional mode

Auto sign (default)

Session.AUTO_ACKNOWLEDGE

Manual sign

Session.CLIENT_ACKNOWLEDGE
client call acknowledgemethod manual sign

Signing generally takes effect in a non-transactional state, and is mainly for consumers. The producer's sign-off function is meaningless, because if the transaction is opened, the sign-off depends on whether it is submitted and rolled back.

There are four states of receipt, and there are two most commonly used states of receipt.

In the non-transactional state, if a manual sign is set, it must be signed after recieve receives a message, otherwise the message is still unconsumed, and the unsigned message can still be obtained during the next consumption.

Allow duplicate messages


Session.DUPS_OK_ACKNOWLEDGE

The message can be confirmed repeatedly, which means that in this mode, there may be repeated messages, and it is not that a message needs to send multiple ACKs. It is a potential "AUTO_ACK" confirmation mechanism, born for batch confirmation, and has the characteristics of "delayed" confirmation. For developers, the code structure in this mode is the same as AUTO_ACKNOWLEDGE, and there is no need to call the acknowledge() method to confirm the message like CLIENT_ACKNOWLEDGE.

 In transaction mode, the
consumer transaction is turned on. Only after commit can all messages become consumed. Writing ack has no effect. Even if you write a sign, it will not sign out

 

 Connection
with transactions In transactional conversations, when a transaction is successfully committed, the message is automatically signed. If the transaction is rolled back, the message will be sent again. In a
non-transactional session, when the message is confirmed depends on the acknowledgement mode when the session is created (acknowledgement)
 

Summary: Four ways to ensure message reliability in ActiveMQ

 

1) Persistence of messages: queue defaults to persistence, and manual settings are required in topic mode

2) Open the transaction of the message

3) In the non-transactional state of the consumer, the manual message signing function should be turned on

4) Cluster construction to achieve high availability

 

Guess you like

Origin blog.csdn.net/m0_46405589/article/details/115078495