普通网友 2017-03-06 13:23
浏览 2490
已采纳

如何使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)
}
  • 写回答

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.

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

    报告相同问题?

    悬赏问题

    • ¥15 我现在有一些关于提升机故障的专有文本数据,量也不多,我在label studio上进行了关系和实体的标注,完成了知识图谱的构造,那么我使用生成式模型的话,我能做哪些工作来写我的论文?
    • ¥15 电脑连不上无线网络如下诊断反馈应该如何操作
    • ¥15 telegram api 使用forward_messages方法转发消息时,目标群组里面会出现此消息来源,如何隐藏?
    • ¥15 在ubuntu中无法连接到远程服务器传输文件
    • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
    • ¥15 Oracle触发器字段变化时插入指定值
    • ¥15 docker无法进入容器内部
    • ¥15 qt https 依赖openssl 静态库
    • ¥15 python flask 报错
    • ¥15 改个密码引发的项目启动问题