The right consumer message confirmation mechanism of RabbitMQ- news

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/zhuzhezhuzhe1/article/details/80709737

The previous section we talked about how to ensure the accuracy of news releases, today we look at how to ensure proper consumer message.

We improve the consumer (Warehouse services) on the basis of the previous.

Modify the configuration file application.yml

Consumer ack default mode is automatic, which means that once a message is consumed (whether successful treatment), the message will be confirmed and will be removed from the queue. This means that when the message processing failure will be deleted from the queue, this is definitely not what we expected. We hope that when the message is correct consumer, the message removed from the queue, otherwise, the message can not be deleted, the message should continue to be consumed, until it succeeds consumption.

So the first thing we will ack is set to manual mode:

spring:
  rabbitmq:
    host: xxx.xxx.xxx.xx
    port: 5672
    username: xxxx
    password: xxxx
    listener:
      direct:
        acknowledge-mode: manual   # 配置该消费者的ack方式为手动

After the success of the consumer to manually confirm

Direct confirmation process is successful, when the process fails, the message back into the queue.


    
    
  1. package com.space.rbq.store.consumer;
  2. import com.google.gson.Gson;
  3. import com.rabbitmq.client.Channel;
  4. import com.space.rbq.store.bean.Order;
  5. import com.space.rbq.store.service.StoreService;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.amqp.core.Message;
  8. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11. import java.io.IOException;
  12. /**
  13. * 负责接收处理订单服务发送的消息
  14. * @author zhuzhe
  15. * @date 2018/6/7 10:09
  16. */
  17. @Slf4j
  18. @Component
  19. public class OrderConsumer {
  20. @Autowired
  21. private StoreService storeService;
  22. /*对列名称*/
  23. public final String QUEUE_NAME1 = "first-queue";
  24. /**
  25. * queues 指定从哪个队列(queue)订阅消息
  26. * @param message
  27. * @param channel
  28. */
  29. @RabbitListener(queues = {QUEUE_NAME1})
  30. public void handleMessage(Message message,Channel channel) throws IOException {
  31. try {
  32. // 处理消息
  33. System.out.println( "OrderConsumer {} handleMessage :"+message);
  34. // 执行减库存操作
  35. storeService.update( new Gson().fromJson( new String(message.getBody()),Order.class));
  36. /**
  37. * 第一个参数 deliveryTag:就是接受的消息的deliveryTag,可以通过msg.getMessageProperties().getDeliveryTag()获得
  38. * 第二个参数 multiple:如果为true,确认之前接受到的消息;如果为false,只确认当前消息。
  39. * 如果为true就表示连续取得多条消息才发会确认,和计算机网络的中tcp协议接受分组的累积确认十分相似,
  40. * 能够提高效率。
  41. *
  42. * 同样的,如果要nack或者拒绝消息(reject)的时候,
  43. * 也是调用channel里面的basicXXX方法就可以了(要指定tagId)。
  44. *
  45. * 注意:如果抛异常或nack(并且requeue为true),消息会重新入队列,
  46. * 并且会造成消费者不断从队列中读取同一条消息的假象。
  47. */
  48. // 确认消息
  49. // 如果 channel.basicAck channel.basicNack channel.basicReject 这三个方法都不执行,消息也会被确认
  50. // 所以,正常情况下一般不需要执行 channel.basicAck
  51. // channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
  52. } catch (Exception e){
  53. log.error( "OrderConsumer handleMessage {} , error:",message,e);
  54. // 处理消息失败,将消息重新放回队列
  55. channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
  56. }
  57. }
  58. }
  59. /*
  60. * 消息的标识,false只确认当前一个消息收到,true确认consumer获得的所有消息
  61. * channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
  62. *
  63. * ack返回false,并重新回到队列
  64. * channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
  65. *
  66. * 拒绝消息
  67. * channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
  68. *
  69. */
Thus, if the process fails, handleMessage method will always receive the news until it succeeds consumption.



Source: https: //github.com/zhuzhegithub/rabbitmq

Back to Top

Reproduced, please keep this source (author): https://blog.csdn.net/zhuzhezhuzhe1


Disclaimer: This article is the original article, allowed to reprint, please indicate the original source Reprinted form of a hyperlink, author information and this statement.

https://blog.csdn.net/zhuzhezhuzhe1


Guess you like

Origin www.cnblogs.com/blwy-zmh/p/11772398.html