我正在使用Go编写RabbitMQ的使用者,该程序必须暂停消息使用一段时间,然后恢复以再次使用队列中的消息。
在阅读https://godoc.org/github.com/streadway/amqp文档时,我无法确定我需要在代码中实现的机制。

有可能这样做吗?有例子吗?

我的代码段:

rabbitMQMessages, err = ch.Consume(
        "TestQ",
        "testConsumer",
        false,
        true,
        false,
        false,
        nil,
    )
    failOnError(err, "Failed to register a consumer")

    forever := make(chan bool)

    go func() {
        select {
        case d := <-rabbitMQMessages: // Cheking if messge was recieved
            log.Printf("Received a message: %s", d.Body)
            dotcount := bytes.Count(d.Body, []byte("."))

            err = ch.Flow(false) // Returns error: Exception (540) Reason: "NOT_IMPLEMENTED - active=false
            failOnError(err, "Failed to close channel")

            t := time.Duration(dotcount)
            time.Sleep(t * time.Second)
            log.Printf("Done")

            err = ch.Flow(true)

            d.Ack(false)
        default:
            log.Println("Default section")
        }
    }()

    log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
    <-forever

最佳答案

您应该cancel the consumer,然后在希望恢复使用消息的时间重新运行ch.Consume

注意:RabbitMQ团队监视rabbitmq-users mailing list,并且有时仅在StackOverflow上回答问题。

关于go - 暂时暂停RabbitMQ消费者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55512484/

10-11 22:10
查看更多