gpt4 book ai didi

error-handling - SimpleMessageListenerContainer 错误处理

转载 作者:行者123 更新时间:2023-12-03 07:40:24 27 4
gpt4 key购买 nike

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

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

最佳答案

How can I set up a retry behaviour in this case



没有复杂的连接重试,只是一个简单的 recoveryInterval .假设经纪人不可用是暂时的。 fatal error (例如错误的凭据)会停止容器。

您可以使用一些外部进程来尝试 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

关于error-handling - SimpleMessageListenerContainer 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28342007/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com