Golang RabbitMQ 消息确认:手动确认和自动确认
在 Golang 中,确认 RabbitMQ 消息有两种方式:手动确认和自动确认。
'手动确认':
- 设置 channel 的 Confirm 模式:channel.Confirm(false)
- 将消息发送到队列:channel.Publish(...)
- 等待确认:if confirmed := <-channel.NotifyPublish(make(chan amqp.Confirmation)); confirmed.Ack { // 消息已经被确认 } else { // 消息未被确认 }
'自动确认':
- 设置 channel 的 Confirm 模式:channel.Confirm(true)
- 将消息发送到队列:channel.Publish(...)
- 不需要等待确认,消息会自动被确认。
需要注意的是,在手动确认模式下,必须在确认前发送消息,否则消息将一直处于未确认状态。如果消息在确认前出现问题,可以使用 channel.Close() 方法关闭 channel,未确认的消息会返回到队列中重新处理。
原文地址: http://www.cveoy.top/t/topic/lAzI 著作权归作者所有。请勿转载和采集!