0

是否可以为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;
        }
4

1 回答 1

0

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

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

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

查看1.5 中的新功能

于 2015-10-29T19:00:35.923 回答