【问题标题】:How to set maximum retry attempts for AmqpConnection如何设置 AmqpConnection 的最大重试次数
【发布时间】:2015-10-29 18:06:33
【问题描述】:

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

【问题讨论】:

    标签: java rabbitmq amqp spring-retry


    【解决方案1】:

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

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

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

    what's new in 1.5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2014-06-03
      • 2010-11-03
      相关资源
      最近更新 更多