Question

I am using RabbitMQ covered with Spring AMQP abstraction. So in essence I am using Spring AMQP.

I need to handle conneciton failures. It's fairly easy to achieve this using Lyra when you use raw RabbitMQ classes.

How do you achieve the same in Spring AMQP? I want my code to be unaware of any network problems.

I know Spring handles reconnections by default (in some way), but what I want is a Lyra-style configuration (be it in XML or wherever), so I can define timeout, max retries, backoff etc..

Was it helpful?

Solution

On the consuming side there is no way to configure that - the container will simply retry the connection on a fixed schedule; configurable by setting recoveryInterval in the SimpleMessageListenerContainer which defaults to 5 seconds. There's not much value in configuring a backoff for consumers.

On the publishing side, you can use spring-aop to wrap the RabbitTemplate (AmqpTemplate interface) in a MethodInterceptor that wraps the send*() calls in a RetryTemplate from spring-retry. The RetryTemplate can be configured with all sorts of options, including backoff policy etc.

If you need help with that, I can try to find some time to post a Gist.

EDIT:

Per the comment below - correct, the recoveryInterval is currently not available with the namespace (but you can still define the container as a <bean ... class="...SimpleMessageListenerContainer...>.

However, it was added a few weeks ago to the master branch (commit here). It is available in the 1.3.0.BUILD-SNAPSHOT.

Also, as a result of your question here, I have added a RetryTemplate option to the RabbitTemplate (pull request here). It should be merged soon. The release candidate for 1.3.0 (1.3.0.RC1) is due Friday and the 1.3.0 GA release will follow within a couple of weeks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top