Search code examples
javarabbitmqspring-amqpspring-rabbit

how configure timeouts, retries or max-attempts in differents queues with spring rabbitmq


Is it possible to make these settings for each queue? I have queues that are important so i need a larger number of retries, but have less important queues that I do not want to configure retry, attempt, etc

public Queue newQueue(String name) {
    return new Queue(name, durable, exclusive, autoDelete, arguments);
}

I saw that in the Queue class, it is possible to pass an argument map as the last parameter, but I do not know if it would be here, or via properties.


Solution

  • In my case I had to create a listener factory with a retry interceptor, in the retry interceptor I set the value for max attempts.

    Maybe works for you:

    @Autowired
    private ConnectionFactory connectionFactory;
    
    @Autowired
    private SomeService someService;
    
    @RabbitListener(id = "queueListener", queues = "queueName",
            containerFactory = "listenerContainerFactory")
    @RabbitHandler
    public void notifyLegacyListener(SomeObject obj) {
        someService.doSomething(obj);
    }
    
    @Bean
    public SimpleRabbitListenerContainerFactory listenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jsonMessageConverter());
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(10);
        factory.setAdviceChain(new Advice[] {retries()});
        return factory;
    }
    
    @Bean
    public RetryOperationsInterceptor retries() {
        return RetryInterceptorBuilder.stateless().maxAttempts(Queues.QUEUE_LEGACY.getMaxAttempts())
                .backOffOptions(1000,
                        3.0, 10000)
                .recoverer(new RejectAndDontRequeueRecoverer()).build();
    }
    
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }