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

发布时间 2023-09-25 21:13:38作者: ashet

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(确认),然后删除消息的信息,若是在代码中再手动确认签收,就会造成重复确认错误。

因此我们需要在消费者方法上标识,消息手动确认签收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