如何为AmqpConnection设置最大重试次数

时间:2015-10-29 18:06:33

标签: java rabbitmq amqp spring-retry

是否可以设置rabbitmq连接的重试次数?如果可能的话怎么做?

我正在从我的应用程序连接到AMQP(RabbitMQ),如果rabbitmq已关闭,则抛出AmqpConnectionException并重试10次以再次建立连接。

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

如何将此重试尝试减少3次然后退出。

使用CachingConnectionFactory连接到rabbitmq服务器。

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;
        }

1 个答案:

答案 0 :(得分:0)

(固定)恢复间隔默认为5秒;这可以配置。

从版本1.5开始,您现在可以配置BackOff,例如ExponentialBackOff,以增加重新连接尝试之间的时间。

可以将ExponentialBackOff配置为在一段时间后完全停止重试,此时容器已停止。

请参阅what's new in 1.5