Search code examples
rabbitmqspring-amqpspring-rabbitspring-retry

Spring AMQP message resiliency in case broker connectivity down


I have a Use Case of managing Spring AMQP client message resiliency in case of RabbitMQ server connectivity down,

For the same I have used Spring Retry

RabbitTemplate template = // get template from some bean 
RetryTemplate retryTemplate = new RetryTemplate();
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
// 10 sec 
backOffPolicy.setInitialInterval(10000);
// 1 hr
backOffPolicy.setMultiplier(360.0);
// 1 hr max interval
backOffPolicy.setMaxInterval(3600001);
retryTemplate.setBackOffPolicy(backOffPolicy);
template.setRetryTemplate(retryTemplate);

template.convertAndSend("Direct-Exchange",
        "Test.Routing.Key", AMQPMessage);

But when a try to test it and bring broker down it hangs at template.convertAndSend() and never recovers even when RabbitMQ broker connectivity is restored


Solution

  • Your retry configuration is rather extreme. Did you wait for an hour?

    It works fine for me...

    @SpringBootApplication
    public class So44300651Application implements CommandLineRunner {
    
        public static void main(String[] args) {
            SpringApplication.run(So44300651Application.class, args).close();
        }
    
        @Autowired
        private RabbitTemplate template;
    
        @Override
        public void run(String... args) throws Exception {
            template.convertAndSend("foo");
        }
    
        @Bean
        public RabbitTemplate template(ConnectionFactory cf) {
            RabbitTemplate template = new RabbitTemplate(cf);
            RetryTemplate retry = new RetryTemplate();
            ExponentialBackOffPolicy backOff = new ExponentialBackOffPolicy();
            backOff.setInitialInterval(10_000);
            backOff.setMultiplier(1.5);
            backOff.setMaxInterval(15_000);
            retry.setBackOffPolicy(backOff);
            template.setRetryTemplate(retry);
            return template;
        }
    
    }
    

    Result:

    09:19:45.592 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=0
    09:19:45.603 [main] DEBUG o.s.r.b.ExponentialBackOffPolicy - Sleeping for 10000
    09:19:55.607 [main] DEBUG o.s.retry.support.RetryTemplate - Checking for rethrow: count=1
    09:19:55.607 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=1
    09:19:55.608 [main] DEBUG o.s.r.b.ExponentialBackOffPolicy - Sleeping for 15000
    09:20:10.610 [main] DEBUG o.s.retry.support.RetryTemplate - Checking for rethrow: count=2
    09:20:10.610 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=2
    09:20:10.654 [main] INFO  o.s.a.r.c.CachingConnectionFactory - Created new connection: SimpleConnection@13cf7d52 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 56958]