RabbitMQ summary (1)-message queue RabbitMQ response mode (automatic, manual)

Original link

Message queue RabbitMQ response mode (automatic, manual)

In order to ensure that the message will not be lost, RabbitMQ supports message response. The consumer sends a message response, telling RabbitMQ that the message has been received and processed. RabbitMQ can delete it.
If a consumer hangs up but does not send a response, RabbitMQ will understand that the message has not been processed completely, and then hand it over to another consumer to reprocess it. In this way, you can confirm that no messages will be lost even if the consumer occasionally hangs up.
There is no message timeout limit; RabbitMQ will re-deliver only when the consumer hangs up. Even processing a message will take a long time.
The message response is turned on by default. We turn off this mechanism through the displayed setting autoAsk=true. The automatic response is now open, once we complete the task, the consumer will automatically send a response. Notify RabbitMQ that the message has been processed and can be deleted from memory. If the consumer does not send an ACK due to downtime or link failure (unlike ActiveMQ, in RabbitMQ, the message has no concept of expiration), RabbitMQ will resend the message to the next consumer listening in the queue.

 

Case: The
producer code remains unchanged, and the consumer code is used to enable the manual response mode.
channel.basicConsume(QUEUE_NAME, false, defaultConsumer);
Note: The second parameter value is false, which means that the automatic response mechanism of RabbitMQ is turned off, and the response is changed to manual response.
When the message is processed, the response status is returned, and true represents the automatic response mode.
channel.basicAck(envelope.getDeliveryTag(), false);

 

Answer mode: automatic sign~~~

Automatic response: The queue will be told to delete the message regardless of whether the consumer processes the message successfully. If processing the message fails, realize automatic compensation (queue delivery was processed again in the past).

Manual response: After the consumer has processed the business logic, he manually returns an ack (notification) to tell the queue that the processing is complete, and the queue then deletes the message.

The producer code remains unchanged, and the consumer:

package com.toov5.Consumer;
 
import java.io.IOException;
import java.util.concurrent.TimeoutException;
 
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.toov5.utils.MQConnectionUtils;
 
public class Consumer {
  
     //队列名称
        private static final String QUEUE_NAME = "test_queue";
        
        public static void main(String[] args) throws IOException, TimeoutException {
            System.out.println("消费者启动..........");
            //创建新的连接
        Connection connection = MQConnectionUtils.newConnection();
           //创建Channel
            Channel channel = connection.createChannel();
            // 消费者关联队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            
              DefaultConsumer defaultConsumerr = new DefaultConsumer(channel) {
                  //监听获取消息
                    @Override
                    public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
                            byte[] body) throws IOException {
                        String msg =new String(body,"UTF-8");
                        System.out.println("消费者获取生产者消息:"+msg);
                    }
              };
            //牵手模式设置  默认自动应答模式  true:自动应答模式  
              channel.basicConsume(QUEUE_NAME, false, defaultConsumerr);//    fanse手动应答          
              
//            //关闭通道和连接
//             channel.close();
//             connection.close();
        }
}

Answer manually. At this time, the messages in the message queue have not been cleared

Modify as follows:

package com.toov5.Consumer;
 
import java.io.IOException;
import java.util.concurrent.TimeoutException;
 
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.toov5.utils.MQConnectionUtils;
 
public class Consumer {
  
     //队列名称
        private static final String QUEUE_NAME = "test_queue";
        
        public static void main(String[] args) throws IOException, TimeoutException {
            System.out.println("消费者启动..........");
            //创建新的连接
        Connection connection = MQConnectionUtils.newConnection();
           //创建Channel
            final Channel channel = connection.createChannel();
            // 消费者关联队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            
              DefaultConsumer defaultConsumerr = new DefaultConsumer(channel) {
                  //监听获取消息
                    @Override
                    public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
                            byte[] body) throws IOException {
                        String msg =new String(body,"UTF-8");
                        System.out.println("消费者获取生产者消息:"+msg);
                        channel.basicAck(envelope.getDeliveryTag(), false);  //手动应答 告诉消息队列服务器 消费成功
                    }
              };
            //牵手模式设置  默认自动应答模式  true:自动应答模式  
              channel.basicConsume(QUEUE_NAME, false, defaultConsumerr);//    fanse手动应答          
              
//            //关闭通道和连接
//             channel.close();
//             connection.close();
        }
}

 

 This is the end of the consumption

 

Guess you like

Origin blog.csdn.net/lsx2017/article/details/114004718