SimpleMessageListenerContainer错误处理 [英] SimpleMessageListenerContainer Error Handling
问题描述
我正在使用 SimpleMessageListenerContainer
作为通过AMQP进行远程处理的基础。只要能够在进程启动时访问RabbitMQ代理,一切都会顺利进行。但是,如果由于某种原因无法连接(网络中断,权限问题等),则容器只会一直尝试重新建立永久连接。在这种情况下,如何设置重试行为(例如,以指数补偿最多尝试5次,然后中止操作,终止进程)?我看过此,但是没有似乎对我来说容器启动工作。
I'm using a SimpleMessageListenerContainer
as a basis for remoting over AMQP. Everything goes smooth provided that the RabbitMQ broker can be reached at process startup. However, if by any reason it can't be reached (network down, permissions problem, etc...) the container just keeps retrying to connect forever. How can I set up a retry behaviour in this case (for example, try at most 5 times with an exponential backoff and then abort, killing the process)? I've had a look at this, but it doesn't seem to work for me on container startup. Can anyone please shed some light?
至少,我希望能够捕获异常并提供日志消息,而不是打印异常本身
At the very least, I'd like to be able to catch the exception and provide a log message, instead of printing the exception itself as is the default behaviour.
推荐答案
在这种情况下,如何设置重试行为
How can I set up a retry behaviour in this case
没有复杂的连接重试,只是一个简单的 recoveryInterval
。假设是代理不可用是暂时的。致命错误(例如错误的凭据)使容器停止运行。
There is no sophisticated connection retry, just a simple recoveryInterval
. The assumption is that the broker unavailability is temporary. Fatal errors (such as bad credentials) stop the container.
您可以使用一些外部过程尝试 connectionFactory.createConnection()
和 stop()
SimpleMessageListenerContainer
认为应该放弃的时候。
You could use some external process to try connectionFactory.createConnection()
and stop()
the SimpleMessageListenerContainer
when you deem it's time to give up.
您也可以将 CachingConnectionFactory
子类化,覆盖 createBareConnection
来捕获异常并增大 recoveryInterval
,然后在需要时调用 stop()
。
You could also subclass CachingConnectionFactory
, override createBareConnection
catch the exception and increment the recoveryInterval
, then call stop()
when you want.
编辑
从1.5开始,您现在可以配置backOff。这是一个使用Spring Boot的示例。
Since 1.5, you can now configure a backOff. Here's an example using 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
这篇关于SimpleMessageListenerContainer错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!