zoukankan      html  css  js  c++  java
  • RabbitMQ的消息确认机制

    一:确认种类

    RabbitMQ的消息确认有两种。

    一种是消息发送确认。这种是用来确认生产者将消息发送给交换器,交换器传递给队列的过程中,消息是否成功投递。发送确认分为两步,一是确认是否到达交换器,二是确认是否到达队列。

    第二种是消费接收确认。这种是确认消费者是否成功消费了队列中的消息。

    二:消息发送确认

    (1)ConfirmCallback

    通过实现ConfirmCallBack接口,消息发送到交换器Exchange后触发回调。

    RabbitMQ的消息确认机制

     

    使用该功能需要开启确认,spring-boot中配置如下:

    spring.rabbitmq.publisher-confirms = true

    (2)ReturnCallback

    通过实现ReturnCallback接口,如果消息从交换器发送到对应队列失败时触发(比如根据发送消息时指定的routingKey找不到队列时会触发)

    RabbitMQ的消息确认机制

     

    使用该功能需要开启确认,spring-boot中配置如下:

    spring.rabbitmq.publisher-returns = true

    三:消息接收确认

    (1)确认模式

    • AcknowledgeMode.NONE:不确认
    • AcknowledgeMode.AUTO:自动确认
    • AcknowledgeMode.MANUAL:手动确认

    spring-boot中配置方法:

    spring.rabbitmq.listener.simple.acknowledge-mode = manual

    (2)手动确认

    RabbitMQ的消息确认机制

    未确认的消息数

    上图为channel中未被消费者确认的消息数。

    通过RabbitMQ的host地址加上默认端口号15672访问管理界面。

    (2.1)成功确认

    void basicAck(long deliveryTag, boolean multiple) throws IOException;

    deliveryTag:该消息的index

    multiple:是否批量. true:将一次性ack所有小于deliveryTag的消息。

    消费者成功处理后,调用channel.basicAck(message.getMessageProperties().getDeliveryTag(), false)方法对消息进行确认。

    (2.2)失败确认

    void basicNack(long deliveryTag, boolean multiple, boolean requeue)

    throws IOException;

    deliveryTag:该消息的index。

    multiple:是否批量. true:将一次性拒绝所有小于deliveryTag的消息。

    requeue:被拒绝的是否重新入队列。

    void basicReject(long deliveryTag, boolean requeue) throws IOException;

    deliveryTag:该消息的index。

    requeue:被拒绝的是否重新入队列。

    channel.basicNack 与 channel.basicReject 的区别在于basicNack可以批量拒绝多条消息,而basicReject一次只能拒绝一条消息。

    (2.3)消息拒绝后,再次发布消息

    channel.basicPublish(message.getMessageProperties().getReceivedExchange(),
                        message.getMessageProperties().getReceivedRoutingKey(), 
                        MessageProperties.PERSISTENT_TEXT_PLAIN,
                        message.getBody());
  • 相关阅读:
    简易sql拼接工具类(使用StringBuilder实现)
    缓存工具类(使用ConcurrentMap集合实现)
    properties文档读取工具类
    【Codeforces Round #655 (Div. 2)】A-D
    【2020 杭电多校第四场】Go Running 最小点覆盖
    【2020 杭电多校第四场】1002 Blow up the Enemy
    【2020 HDU 多校训练第三场 1007 Tokitsukaze and Rescue】暴力删边&最短路
    【2020杭电多校第二场】Total Eclipse 思维+并查集
    【2020HDU多校】Lead of Wisdom 暴力
    【CF-1371 C-E2】
  • 原文地址:https://www.cnblogs.com/fdzfd/p/9420333.html
Copyright © 2011-2022 走看看