douyihuaimao733955 2019-04-24 09:37
浏览 152
已采纳

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

I'm reading messages from RabbitMQ queue like this:

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

There could be situations when reading from messages fail, like network error, or RabbitMQ's channel was invalidated due to an error. How to detect such errors and get relevant error message?

  • 写回答

1条回答 默认 最新

  • doufei8691 2019-04-24 09:55
    关注

    Assuming that you are using streadway/amqp, this is probably what you are looking for 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 channel or connection exception in the form of a Connection.Close or Channel.Close method. Connection exceptions will be broadcast to all open channels and all channels will be closed, where channel exceptions will only be broadcast to listeners to this channel.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。