gpt4 book ai didi

go - 从 RabbitMQ 队列读取时如何检测错误

转载 作者:IT王子 更新时间:2023-10-29 01:43:44 26 4
gpt4 key购买 nike

我正在像这样从 RabbitMQ 队列中读取消息:

messages, err := channel.Consume(
queue, // queue
queue, // consumer - same as queue
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil) // args
if err != nil {
// handle error
}

loop:
for {
select {
case cmd := <-cmdChannel:
if cmd == cmdStop {
break loop
}
case message := <-messages:
go process(message)
}
}

可能会出现读取消息失败的情况,例如网络错误,或者 RabbitMQ 的 channel 因错误而失效。如何检测此类错误并获取相关错误消息?

最佳答案

假设您正在使用 streadway/amqp,这可能就是您正在寻找的 https://godoc.org/github.com/streadway/amqp#Channel.NotifyClose

func (*Channel) NotifyClose

func (ch *Channel) NotifyClose(c chan *Error) chan *Error

NotifyClose registers a listener for when the server sends a channelor connection exception in the form of a Connection.Close orChannel.Close method. Connection exceptions will be broadcast to allopen channels and all channels will be closed, where channelexceptions will only be broadcast to listeners to this channel.

The chan provided will be closed when the Channel is closed and on agraceful close, no error will be sent.

关于go - 从 RabbitMQ 队列读取时如何检测错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55826930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com