0

Is it possible to set retry attempts for rabbitmq connection? If possible how to do that?

I am currently connecting to AMQP (RabbitMQ) from my application, if rabbitmq is down then AmqpConnectionException is thrown and it retries 10 times to establish connection again.

15:50:41.533 [SimpleAsyncTaskExecutor-173] WARN  o.s.a.r.l.SimpleMessageListenerContainer - **Consumer raised exception,** processing can restart if the connection factory supports it.
 **Exception summary**: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
15:50:41.533 [SimpleAsyncTaskExecutor-173] INFO  o.s.a.r.l.SimpleMessageListenerContainer - Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
15:50:42.565 [SimpleAsyncTaskExecutor-172] WARN  o.s.a.r.l.SimpleMessageListenerContainer - Consumer raised exception, processing can restart if the connection factory supports it.
 **Exception summary**: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
15:50:42.565 [SimpleAsyncTaskExecutor-172] INFO  o.s.a.r.l.SimpleMessageListenerContainer - Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
15:50:43.567 [SimpleAsyncTaskExecutor-17] WARN  o.s.a.r.l.SimpleMessageListenerContainer - Consumer raised exception, processing can restart if the connection factory supports it.
**Exception summary**: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
15:50:43.567 [SimpleAsyncTaskExecutor-17] INFO  o.s.a.r.l.SimpleMessageListenerContainer - Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
15:50:44.581 [SimpleAsyncTaskExecutor-181] WARN  o.s.a.r.l.SimpleMessageListenerContainer - Consumer raised exception, processing can restart if the connection factory supports it.
 **Exception summary:** org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
15:50:44.581 [SimpleAsyncTaskExecutor-181] INFO  o.s.a.r.l.SimpleMessageListenerContainer - Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
15:50:45.589 [SimpleAsyncTaskExecutor-180] WARN  o.s.a.r.l.SimpleMessageListenerContainer - Consumer raised exception, processing can restart if the connection factory supports it.
 **Exception summary**: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
15:50:45.589 [SimpleAsyncTaskExecutor-180] INFO  o.s.a.r.l.SimpleMessageListenerContainer - Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
15:50:47.606 [SimpleAsyncTaskExecutor-179] WARN  o.s.a.r.l.SimpleMessageListenerContainer - Consumer raised exception, processing can restart if the connection factory supports it.
 **Exception summary**: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
15:50:47.606 [SimpleAsyncTaskExecutor-179] INFO  o.s.a.r.l.SimpleMessageListenerContainer - Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
15:50:48.616 [SimpleAsyncTaskExecutor-178] WARN  o.s.a.r.l.SimpleMessageListenerContainer - Consumer raised exception, processing can restart if the connection factory supports it.
 **Exception summary**: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
15:50:48.616 [http-nio-8443-exec-4] ERROR u.c.o.s.v.messaging.config.Sender - Excception has happened connecting to RabbitMQ
15:50:48.626 [SimpleAsyncTaskExecutor-178] INFO  o.s.a.r.l.SimpleMessageListenerContainer - Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0

How to reduce this retry attempts to 3 times and then exit.

Am using CachingConnectionFactory to get connected to rabbitmq server.

public CachingConnectionFactory connectionFactory() {
                CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
            connectionFactory.setHost(env.getProperty("rabbitmq.host"));
            connectionFactory.setPort(Integer.parseInt(env
                    .getProperty("rabbitmq.port")));
            connectionFactory.setUsername(env.getProperty("rabbitmq.user"));
            connectionFactory.setPassword(env.getProperty("rabbitmq.pass"));
            return connectionFactory;
        }
Vishwa
  • 607
  • 2
  • 11
  • 21

1 Answers1

0

The (fixed) recovery interval defaults to 5 seconds; this can be configured.

Starting with version 1.5, you can now configure a BackOff, such as an ExponentialBackOff to increase the times between reconnection attempts.

The ExponentialBackOff can be configured to stop retrying altogether after some time, at which time the container is stopped.

See what's new in 1.5.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179