Search code examples
springspring-bootrabbitmqspring-amqp

How to pause @RabbitListener without destroying AnonymousQueue


I have a Spring Boot application that is generally message driven but on special occasions, the incoming messages need to be stopped. However I cannot loose those messages, I need to buffer them and receive them in the correct order later.

There are myriads of questions asked and answered about stopping the Listener via the ListernerEndpointRegistry, such as here.

However when I stop the container the AnonymousQueue seems to disappear. I want the queue to stay on the exchange and buffer any messages, and receive them, when I restart. Is this possible or do I need to buffer them inside my application?


Solution

  • There are two options.

    1. Don't use an anonymous (auto-delete) queue and use stop/start.
    2. Don't stop the container; simply block the listener thread(s) when you want to suspend message delivery and wake them when you want to restart.

    However I cannot lose those messages

    If you cannot lose messages, you should NEVER use an auto-delete queue - you can lose messages at any time if you have a simple network glitch.