Is it possible to somehow postpone listening messages from some particular queue in spring-amqp ?
In my use case I have a service which has to listen messages on two RabbitMQ queues. The first one is exclusively for this service, the second one is used for load balancing jobs by multiple instances of my service (running on different machines).
My service on statup receive configuration via first queue and configure itself. Only after that configuration it is allowed to process "standard" jobs from second queue - not before.
How can I achieve this ? Using @RabbitListener(queues = {queue1,queue2}) starts listening immediatelly.
I have also looked on rabbitmq_delayed_message_exchange, but this is not what I want as it delays processing of messages. I do not want to delay processing (the other already configured consumers can process work).
Thank you for any help.
The @RabbitListener
has an autoStartup
option:
/**
* Set to true or false, to override the default setting in the container factory.
* @return true to auto start, false to not auto start.
* @since 2.0
*/
String autoStartup() default "";
I guess it would be better for you to have two separate @RabbitListener
s: one for configuration queue and another not-autoStartup'ed. When configuration is ready, you need to obtain a container for the second one from the RabbitListenerEndpointRegistry.getListenerContainer()
and call its start()
. The id
you may also configure on that second @RabbitListener
:
/**
* The unique identifier of the container managing for this endpoint.
* <p>If none is specified an auto-generated one is provided.
* @return the {@code id} for the container managing for this endpoint.
* @see org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry#getListenerContainer(String)
*/
String id() default "";
See documentation for more info: https://docs.spring.io/spring-amqp/docs/2.1.7.RELEASE/reference/html/#async-annotation-driven