RabbitMq在使用手动确认消息Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=

RabbitMq在使用手动确认消息 报错: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)

2024-05-30 09:11:22.904 ERROR 27772 --- [168.65.128:5672] o.s.a.r.c.CachingConnectionFactory       : 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)
2024-05-30 09:11:23.911  INFO 27772 --- [ntContainer#0-3] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@479077bf: tags=[[amq.ctag-rYQRhRCUu7vBoDA547EtPg]], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.65.128:5672/,3), conn: Proxy@24fcfdb Shared Rabbit Connection: SimpleConnection@367a245e [delegate=amqp://admin@192.168.65.128:5672/, localPort= 2941], acknowledgeMode=AUTO local queue size=0

在这里插入图片描述
PRECONDITION_FAILED - unknown delivery tag 1 意味着消息已经确认过
默认情况下 RabbitMQ 是自动ACK(确认签收)机制,就意味着 MQ 会在消息发送完毕后,自动帮我们去ACK(确认),若是在代码中再手动确认签收,就会造成确认错误。
因此我们需要在消费者方法上标识,消息手动确认签收ackMode = "MANUAL"
解决方案:
@RabbitListener上加入设置应答模式(ackMode )为手动,即:

  @RabbitListener(queues = RabbitMQConfig.SECOND_QUEUE,ackMode = "MANUAL")

代码:

@RabbitListener(queues = RabbitMQConfig.SECOND_QUEUE,ackMode = "MANUAL")
    public void receiveMessage(Message message, Channel channel, String msg) throws IOException {
        log.info("收到消息:{}", msg);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        log.info("deliveryTag: {}", deliveryTag);
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            channel.basicNack(deliveryTag,false,true);
            e.printStackTrace();
        }
    }
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值