How to Lazy Load RabbitMQ queue using @Lazy in spring boot?

Viewed 533

Actually in my RabbitMQ config I have declared 10 consumers for each queue. So all consumer threads are created before my Spring Boot application is fully up so it is taking time on application startup.

I want to lazy load all my Rabbitmq queue when my project is up. I tried using @Lazy on the configuration class but it does not seem working.

Is there any way to declare lazy load queue ?

2 Answers

All you need to do is set "autoStartup=false" to @RabbitListener:

@RabbitListener(queues = "${yourqueue}",autoStartup = "false")

And then find all registered rabbit listeners and start it:

    public static void main(String[] args) {

    ConfigurableApplicationContext run = SpringApplication.run(SimPurchaseApplication.class, args);

    RabbitListenerEndpointRegistry rabbitListeners = run.getBean(RabbitListenerEndpointRegistry.class);
    for (MessageListenerContainer listenerContainer : rabbitListeners.getListenerContainers()){
        listenerContainer.start();
    }
}
Related