RabbitMQ的ACK与重回队列机制

2020-07-14  本文已影响0人  OxygenPlus
消费端的手工ACK与NACK

当我们设置 autoACK=false 时,就开启了手工ACK模式,那么其实手工模式包括了手工ACKNACK

相关方法:

  1. NACK:
    方法:void basicNack(long deliveryTag, boolean multiple, boolean requeue)
  2. 如果由于服务器宕机等严重问题,那我们就需要手工进行 ACK 保障消费端消费成功!
    方法:void basicAck(long deliveryTag, boolean multiple)
消费端的重回队列

代码演示

生产端:

public class Producer { 
    public static void main(String[] args) throws Exception {   
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.11.76");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        
        Connection connection = connectionFactory.newConnection();
        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<>();
            headers.put("num", i);
            // deliveryMode = 1 不持久化,deliveryMode = 2 持久化
            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());
        }
        
    }
}

消费端:

public class Consumer {
    public static void main(String[] args) throws Exception {       
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.11.76");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        
        Connection connection = connectionFactory.newConnection();
        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 MyConsumer(channel));
    }
}

MyConsumer:

public class MyConsumer extends DefaultConsumer {
    private Channel channel ;
    
    public MyConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }

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

上一篇 下一篇

猜你喜欢

热点阅读