2.4 RabbitMQ高级特效-消息的ACK与重回队列

消费端的手工ACK和NACK
消费端进行消费的时候,如果由于业务异常我们可以进行日志的记录,然后进行补偿。如果由于服务器宕机等严重问题,那么我们就需要手工进行ACK保障消费端消费成功。
消费端的重回队列
消费端重回队列是为了对没有处理成功的消息,把消息重新会递给Broker!一般我们在实际应用中,都会关闭重回队列,也就是设置未False。

自定义消费者代码

package com.star.movie.ack;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

/**
 * @Description:
 * @author:kaili
 * @Date: 2019-04-22 18:47
 **/
public class MyAckConsumer extends DefaultConsumer {

    private Channel channel ;

    public MyAckConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        String headerMsg = "num";
        System.err.println("-----------consume message----------");
        System.err.println("body: " + new String(body));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if((Integer)properties.getHeaders().get(headerMsg) == 0) {
            channel.basicNack(envelope.getDeliveryTag(), false, true);
        } else {
            channel.basicAck(envelope.getDeliveryTag(), false);
        }
    }
}

消费端的代码,对队列中的消息获取head中的属性num值,如果num为0就调用nack方法观察效果

消费者端代码

package com.star.movie.ack;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.star.movie.common.Constant;

/**
 * @Description:
 * @author:kaili
 * @Date: 2019-04-22 18:46
 **/
public class AckConsumer {
    public static void main(String[] args) throws Exception{
        Connection connection = Constant.getConnection();
        Channel channel = connection.createChannel();


        String exchangeName = "test_ack_exchange";
        String queueName = "test_ack_queue";
        String routingKey = "ack.#";

        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        // 手工签收 必须要关闭 autoAck = false
        channel.basicConsume(queueName, false, new MyAckConsumer(channel));
    }
}

手工签收 必须要关闭 autoAck = false

生产者代码

package com.star.movie.ack;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.star.movie.common.Constant;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author:kaili
 * @Date: 2019-04-22 18:45
 **/
public class AckProducer {
    public static void main(String[] args) throws Exception{
        Connection connection = Constant.getConnection();
        Channel channel = connection.createChannel();
        String exchange = "test_ack_exchange";
        String routingKey = "ack.save";
        for(int i =0; i<5; i ++){
            Map<String, Object> headers = new HashMap<String, Object>();
            headers.put("num", i);
            AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                    .deliveryMode(2)
                    .contentEncoding("UTF-8")
                    .headers(headers)
                    .build();
            String msg = "Hello RabbitMQ ACK Message " + i;
            channel.basicPublish(exchange, routingKey, true, properties, msg.getBytes());
        }
    }
}

step 1 启动消费者端代码
生成类型为topic的交换机test_ack_exchange,绑定test_ack_queue队列,绑定健为ack.#
2.4 RabbitMQ高级特效-消息的ACK与重回队列_第1张图片

step 2 启动生产者端代码

观察消费者端控制台打印的信息:

-----------consume message----------
body: Hello RabbitMQ ACK Message 0
-----------consume message----------
body: Hello RabbitMQ ACK Message 1
-----------consume message----------
body: Hello RabbitMQ ACK Message 2
-----------consume message----------
body: Hello RabbitMQ ACK Message 3
-----------consume message----------
body: Hello RabbitMQ ACK Message 4
-----------consume message----------
body: Hello RabbitMQ ACK Message 0
-----------consume message----------
body: Hello RabbitMQ ACK Message 0
-----------consume message----------
body: Hello RabbitMQ ACK Message 0
-----------consume message----------
body: Hello RabbitMQ ACK Message 0

一直在消费head中num为0的消息,因为这条消息使用的是nack,并且将消息重回队列了,所以会这样一直消费。

step 3关闭消费者端代码
观察test_ack_queue队列
2.4 RabbitMQ高级特效-消息的ACK与重回队列_第2张图片
该队列中还有一条消息待消费,正是我们让它重回队列的那条消息

step 4 将重回队列修改为false
修改自定义消费者中的代码

channel.basicNack(envelope.getDeliveryTag(), false, false);

启动生产者代码,然后启动消费者代码,观察消费者端的控制台消息

-----------consume message----------
body: Hello RabbitMQ ACK Message 0
-----------consume message----------
body: Hello RabbitMQ ACK Message 0
-----------consume message----------
body: Hello RabbitMQ ACK Message 1
-----------consume message----------
body: Hello RabbitMQ ACK Message 2
-----------consume message----------
body: Hello RabbitMQ ACK Message 3
-----------consume message----------
body: Hello RabbitMQ ACK Message 4

num为0的消息消费后未重回Broker

你可能感兴趣的:(RabbitMQ学习)