0

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

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Akshat
  • 575
  • 2
  • 12
  • 28

1 Answers1

2

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]
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Russel , Solution looks good , But from resiliency point of view if the RabbitMQ service is not available for say 1 hour ( Which generally is not case , But can happen very rarely ), Does that setting overkill an application ? – Akshat Jun 05 '17 at 07:17
  • All I am saying is that waiting for an hour between attempts is too long; most people will try every few seconds, or perhaps 1 minute at most. – Gary Russell Jun 05 '17 at 13:41