SimpleMessageListenerContainer错误处理

马丁

我使用aSimpleMessageListenerContainer作为通过AMQP进行远程处理的基础。只要能够在进程启动时访问RabbitMQ代理,一切都会顺利进行。但是,如果由于某种原因而无法访问它(网络中断,权限问题等),则容器将一直尝试重新尝试永久连接。在这种情况下,如何设置重试行为(例如,以指数补偿最多尝试5次,然后中止并终止进程)?我有一个看看这个,但它似乎并没有为我工作在容器启动时没有。谁能给我一些启示?

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

加里·罗素

在这种情况下,如何设置重试行为

没有复杂的连接重试,只有一个简单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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章