RabbitMQ报错:Shutdown Signal: channel error; protocol method

Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 1, class-id=60, method-id=80)

默认情况下 RabbitMQ 是自动ACK(确认签收)机制,就意味着 MQ 会在消息发送完毕后,自动帮我们去ACK(确认),若是在代码中再手动确认签收,就会造成确认错误。

"PRECONDITION_FAILED - unknown delivery tag" 表明交付标签(delivery tag)不合法或已经被确认过

因此我们需要在订阅者(消费者)的方法上标识,消息手动确认签收ackMode = "MANUAL",代码如下

    @RabbitListener(queues = MqConf.HELLO_QUEUE, ackMode = "MANUAL")
    public void handleMessage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("----> Received message: " + new String(message.getBody()));
            log.info("deliveryTag: {}", deliveryTag);
            // 手动确认签收
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            // 拒绝签收,消息重新入队
            channel.basicReject(deliveryTag, true);
        }
    }

在application.yml文件中配置的手动签收不知为何失效

  rabbitmq:
    host: xxx
    port: 5672
    username: guest
    password: guest
    listener:
      direct:
        acknowledge-mode: manual

 

posted @ 2023-09-25 21:04  Ashe|||^_^  阅读(4091)  评论(4编辑  收藏  举报