SimpleMessageListenerContainer 错误处理

SimpleMessageListenerContainer Error Handling

我正在使用 SimpleMessageListenerContainer 作为通过 AMQP 进行远程处理的基础。只要在进程启动时可以访问 RabbitMQ 代理,一切都会顺利进行。但是,如果由于任何原因无法访问(网络中断、权限问题等),容器将永远重试连接。在这种情况下如何设置重试行为(例如,最多尝试 5 次指数退避,然后中止,终止进程)?我看过 this,但它似乎对容器启动不起作用。谁能解释一下?

至少,我希望能够捕获异常并提供日志消息,而不是像默认行为那样打印异常本身。

How can I set up a retry behaviour in this case

没有复杂的连接重试,只是简单的recoveryInterval。假设代理不可用是暂时的。致命错误(例如凭据错误)会停止容器。

您可以使用一些外部进程来尝试 connectionFactory.createConnection()stop() 以及 SimpleMessageListenerContainer,当您认为是时候放弃时。

您还可以子类化 CachingConnectionFactory,覆盖 createBareConnection 捕获异常并递增 recoveryInterval,然后在需要时调用 stop()

编辑

从 1.5 开始,您现在可以配置 backOff。这是一个使用 Spring Boot...

的示例
@SpringBootApplication
public class RabbitBackOffApplication {

    public static void main(String[] args) {
        SpringApplication.run(RabbitBackOffApplication.class, args);
    }

    @Bean(name = "rabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        BackOff recoveryBackOff = new FixedBackOff(5000, 3);
        factory.setRecoveryBackOff(recoveryBackOff);
        return factory;
    }

    @RabbitListener(queues = "foo")
    public void listen(String in) {

    }

}

2018-04-16 12:08:35.730  INFO 84850 --- [           main] com.example.RabbitBackOffApplication     : Started RabbitBackOffApplication in 0.844 seconds (JVM running for 1.297)
2018-04-16 12:08:40.788  WARN 84850 --- [cTaskExecutor-1] 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 (Connection refused)
2018-04-16 12:08:40.788  INFO 84850 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@57abad67: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
2018-04-16 12:08:40.789  INFO 84850 --- [cTaskExecutor-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:1234]
2018-04-16 12:08:45.851  WARN 84850 --- [cTaskExecutor-2] 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 (Connection refused)
2018-04-16 12:08:45.852  INFO 84850 --- [cTaskExecutor-2] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@3479ea: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
2018-04-16 12:08:45.852  INFO 84850 --- [cTaskExecutor-3] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:1234]
2018-04-16 12:08:50.935  WARN 84850 --- [cTaskExecutor-3] 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 (Connection refused)
2018-04-16 12:08:50.935  INFO 84850 --- [cTaskExecutor-3] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@2be60f67: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
2018-04-16 12:08:50.936  INFO 84850 --- [cTaskExecutor-4] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:1234]
2018-04-16 12:08:50.938  WARN 84850 --- [cTaskExecutor-4] o.s.a.r.l.SimpleMessageListenerContainer : stopping container - restart recovery attempts exhausted