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();
}
}