Skip to main content
 首页 » 编程设计

RabbitMQ 使用立即位和强制位

2024年05月06日44三少

我正在使用 RabbitMQ 服务器。

为了发布消息,我将 immediate 字段设置为 true 并尝试发送 50,000 条消息。使用rabbitmqctl list_queues,我看到队列中的消息数量

然后,我将 immediate 标志更改为 false 并再次尝试发送 50,000 条消息。使用rabbitmqctl list_queues,我看到队列中总共有 100,000 条消息(到目前为止,没有消费者存在)。

之后,我启动了一个消费者,它消耗了所有 100,000 条消息。

任何人都可以帮助我理解立即位字段和这种行为吗?另外,我无法理解强制位字段的概念。

请您参考如下方法:

immediatemandatory 字段是 AMQP 规范的一部分,并且也在 RabbitMQ 常见问题解答中进行了介绍,以阐明其实现者如何解释其含义:

Mandatory

This flag tells the server how to react if a message cannot be routed to a queue. Specifically, if mandatory is set and after running the bindings the message was placed on zero queues then the message is returned to the sender (with a basic.return). If mandatory had not been set under the same circumstances the server would silently drop the message.

或者用我的话来说,“将此消息放入至少一个队列。如果不能,请将其发送回给我。”

Immediate

For a message published with immediate set, if a matching queue has ready consumers then one of them will have the message routed to it. If the lucky consumer crashes before ack'ing receipt the message will be requeued and/or delivered to other consumers on that queue (if there's no crash the messaged is ack'ed and it's all done as per normal). If, however, a matching queue has zero ready consumers the message will not be enqueued for subsequent redelivery on from that queue. Only if all of the matching queues have no ready consumers that the message is returned to the sender (via basic.return).

或者用我的话说,“如果至少有一个消费者连接到我的队列,此时可以立即传递消息,请立即将此消息传递给他们。如果没有消费者连接,那么就没有点在于我的消息稍后被消耗,而他们永远不会看到它。他们打瞌睡,他们就输了。”