Skip to main content
 首页 » 编程设计

RabbitMQ 使用立即位和强制位


我正在使用 RabbitMQ 服务器。

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

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

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



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


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.



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).