RabbitMQ use of immediate and mandatory bits

2019-03-11 14:46发布

问题:

I've used RabbitMQ server and for publishing messages when the immediate field was set true ,I tried sending 50,000 messages and using rabbitmqctl list_queues, I saw that number of messages in the queue was zero. Then I changed the immediate flag to false and again tried sending 50,000 messages and then using rabbitmqctl list_queues i saw that total 100,000 messages were in queues.(Till now no consumer was present)

And after that I started consumer and it consumed all the 100,000 messages.Can anybody please help me in understanding about immediate bit field and this behavior too. Also, I could not understand the concept of mandatory bit field.

Thanking in Advance.

Gurpreet Singh.

回答1:

The immediate and mandatory fields are part of the AMQP specification, and are also covered in the RabbitMQ FAQ to clarify how its implementers interpreted their meaning:

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.

Or in my words, "Put this message on at least one queue. If you can't, send it back to me."

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

Or in my words, "If there is at least one consumer connected to my queue that can take delivery of a message right this moment, deliver this message to them immediately. If there are no consumers connected then there's no point in having my message consumed later and they'll never see it. They snooze, they lose."



回答2:

http://www.rabbitmq.com/blog/2012/11/19/breaking-things-with-rabbitmq-3-0/

Removal of "immediate" flag

What changed? We removed support for the rarely-used "immediate" flag on AMQP's basic.publish.

Why on earth did you do that? Support for "immediate" made many parts of the codebase more complex, particularly around mirrored queues. It also stood in the way of our being able to deliver substantial performance improvements in mirrored queues.

What do I need to do? If you just want to be able to publish messages that will be dropped if they are not consumed immediately, you can publish to a queue with a TTL of 0.

If you also need your publisher to be able to determine that this has happened, you can also use the DLX feature to route such messages to another queue, from which the publisher can consume them.

Just copied the announcement here for a quick reference.



标签: rabbitmq amqp