doulinduansh14199
2017-03-06 13:23
浏览 1.5k
已采纳

如何使rabbitmq消费者超时?

I have rabbitmq consuming the queue but once client is subscribed it stays consuming queue forever. Is there a timeout to declare and exit i.e. after queue is empty ?

 msgs, err := ch.Consume(
                q.Name, // queue
                "",     // consumer
                true,   // auto-ack
                false,  // exclusive
                false,  // no-local
                false,  // no-wait
                nil,    // args
        )
for msg := range msgs { 
                log.Printf("Received message with message: %s", msg.Body)
}

图片转代码服务由CSDN问答提供 功能建议

我让Rabbitmq占用了队列,但是一旦订阅了客户端,它将永远保持消耗队列。 是否有超时时间来声明和退出,即在队列为空之后?

  msgs,err:= ch.Consume(
 q.Name,//队列
“”  ,//消费者
 true,//自动确认
 false,//排他
 false,// no-local 
 false,//不等待
 nil,// args 
)
for  msg:=范围msgs {
 log.Printf(“接收到的消息,带有消息:%s”,msg.Body)
} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douwo6738 2017-03-06 15:32
    已采纳

    You can use the standard Go pattern for timing out.

    Here is a working example.

    const duration = 3 * time.Second
    timer := time.NewTimer(duration)
    for {
        select {
        case d := <-msgs:
            timer.Reset(duration)
            fmt.Printf("Received a message: %s
    ", d.Body)
        case <- timer.C:
            fmt.Println("Timeout !")
            os.Exit(1)
        }
    }
    

    It probably needs some polishing, e.g. I suppose it would be better to stop the timer when you receive the message and enable it again when you are done processing it, but this should get you started.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题