2 回答
TA贡献1811条经验 获得超5个赞
该字段q2.Messages
不可靠,它是未等待确认的消息的计数,即已确认的消息。
你的消费者被声明为autoAck = true
——即noAck
——,这意味着不需要确认,这意味着已经确认了零个消息。
当您注释掉消费者时,确认消息的数量可能取决于发布者缓冲区。
使用 AMQP 0.9.1 以编程方式在给定队列上获取精确数量的消息基本上是不可能的。您可以改用message_stats
管理 API 中的字段:
http://localhost:15672/api/queues/vhost/queue_name
TA贡献1898条经验 获得超8个赞
接受的解决方案将是黑绿色的。证明是下面的替换,只需将问题部分中的消费者和发布者代码替换为:
// Listen
eventQueue, err := ch.Consume(
q.Name, // queue
"", // consumer
false, // auto-ack <-- Difference
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
panic(err)
}
go func() {
for a := range eventQueue {
err = ch.Ack(a.DeliveryTag, false) // <-- Difference
if err != nil {
panic(err)
}
fmt.Printf("Received Event %s\n", string(a.Body))
time.Sleep(time.Second * 4)
}
}()
go func() {
count := 0
for {
err = ch.Publish(exchangeName, "", false, false, amqp.Publishing{
ContentType: "application/json",
Body: []byte(fmt.Sprintf("Message %d", count)),
})
fmt.Printf("Sent Message %d\n", count)
count++
if err != nil {
panic(err)
}
if count >= 20 { // <-- Difference
break
}
time.Sleep(time.Second * 2)
}
}()
输出:
.... The increase in the queue length
Sent Message 13
Queue Len: 8.000000 - 0
Queue Len: 8.000000 - 0
Received Event Message 4
Sent Message 14
Queue Len: 8.000000 - 0
Queue Len: 9.000000 - 0
Sent Message 15
Queue Len: 9.000000 - 0
Queue Len: 9.000000 - 0
Received Event Message 5
Sent Message 16
Queue Len: 9.000000 - 0
Queue Len: 9.000000 - 0
Sent Message 17
Queue Len: 11.000000 - 0
Queue Len: 11.000000 - 0
Received Event Message 6
Sent Message 18
Queue Len: 11.000000 - 0
Queue Len: 11.000000 - 0
Sent Message 19
Queue Len: 11.000000 - 0
Queue Len: 12.000000 - 0
Received Event Message 7
Queue Len: 12.000000 - 0
Queue Len: 12.000000 - 0
Queue Len: 12.000000 - 0
Queue Len: 12.000000 - 0
Received Event Message 8
Queue Len: 12.000000 - 0
Queue Len: 12.000000 - 0
Queue Len: 12.000000 - 0
Queue Len: 12.000000 - 0
Received Event Message 9
Queue Len: 12.000000 - 0
Queue Len: 11.000000 - 0
Queue Len: 11.000000 - 0
Queue Len: 11.000000 - 0
Received Event Message 10
Queue Len: 11.000000 - 0
Queue Len: 11.000000 - 0
Queue Len: 10.000000 - 0
Queue Len: 10.000000 - 0
Received Event Message 11
Queue Len: 10.000000 - 0
Queue Len: 10.000000 - 0
Queue Len: 10.000000 - 0
Queue Len: 9.000000 - 0
Received Event Message 12
....
As publisher exits it decreases, the consumer catches up and message len decreases:
Received Event Message 16
Queue Len: 5.000000 - 0
Queue Len: 5.000000 - 0
Queue Len: 5.000000 - 0
Queue Len: 4.000000 - 0
Received Event Message 17
Queue Len: 4.000000 - 0
Queue Len: 4.000000 - 0
Queue Len: 4.000000 - 0
Queue Len: 4.000000 - 0
Received Event Message 18
Queue Len: 2.000000 - 0
Queue Len: 2.000000 - 0
Queue Len: 2.000000 - 0
Queue Len: 2.000000 - 0
Received Event Message 19
Queue Len: 2.000000 - 0
Queue Len: 1.000000 - 0
- 2 回答
- 0 关注
- 188 浏览
添加回答
举报