问题
Is is possible to configure a RabbitMQ exchange or a queue in such a way that at most one message with a given routing key is pending at any time? If new message arrives, the old one would be dropped and the new one enqueued.
If such option is not available, what would be the best way to implement this at the application level? I.e. when application receives a message how can it check if there any more pending messages?
回答1:
You need to install Last Value Cache and enable it. Your exchange will be type "x-lvc", which inherits from the direct exchange type.
each time you connect to MQ, create a queue and bind to this exchange. It will deliver the most recent message to the queue. It is perfect for making sure you get only the most uptodate message. All other messages sent to this exchange are discarded unless there is a queue connected. So once connected you will continue to receive updates.
here are installation instructions: https://github.com/simonmacmullen/rabbitmq-lvc-plugin
here is a similar question: RabbitMQ messaging - initializing consumer
来源:https://stackoverflow.com/questions/13451401/configure-rabbitmq-to-replace-an-old-pending-message-with-a-new-one