Search code examples
rabbitmqspring-rabbit

Spring Amqp - Is it possible to hook a custom handler before a basicGet()?


I have a message handler which is annotated with @RabbitHandler which picks up and process messages from the queue.

Use case

During deployment times, the instance should not be picking any messages from the queue. Is there some place where I can hook and check if my instance is blocked (read from db to check) and stop receiving messages. I checked that we have setAfterReceivePostProcessors but what I am actually looking for is something like beforeReceiveProcessor to solve my use case of checking and not polling at all. It should not be reaching my @RabbitHandler method at all. Is it possible to achieve this level of control ?

If not, is the only possible way is to write a custom poller ?


Solution

  • The infrastructure behind @RabbitListener uses basicConsume(), not basicGet().

    You can control the lifecycle of the listener container using the RabbitListenerEndpointRegistry bean.

    @RabbitListener(id = "foo", ..., autoStartup = "false")
    

    Then

    registry.getListenerContainer("foo").start();
    

    You can use a property placeholder too.

    @RabbitListener(id = "foo", ..., autoStartup = "${should.start:false}")