2017-06-01 46 views
1

當我停止RabbitMQ的服務器,下方將引發異常:如何在Spring AMQP框架下捕獲AmqpConnectException?

org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect 
    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62) ~[spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:306) ~[spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:547) ~[spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1411) ~[spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1392) ~[spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1368) ~[spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:336) ~[spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.redeclareElementsIfNecessary(SimpleMessageListenerContainer.java:1123) [spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$800(SimpleMessageListenerContainer.java:98) [spring-rabbit-1.6.6.RELEASE.jar:na] 
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1329) [spring-rabbit-1.6.6.RELEASE.jar:na] 
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_121] 
Caused by: java.net.ConnectException: Connection refused: connect 
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_121] 
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_121] 
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_121] 
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_121] 
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_121] 
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_121] 
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_121] 
    at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_121] 
    at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:47) ~[amqp-client-3.6.5.jar:na] 
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:822) ~[amqp-client-3.6.5.jar:na] 
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:778) ~[amqp-client-3.6.5.jar:na] 
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:898) ~[amqp-client-3.6.5.jar:na] 
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:297) ~[spring-rabbit-1.6.6.RELEASE.jar:na] 
    ... 9 common frames omitted 

我怎麼能捕獲此異常?我應該創建還是實現一些處理程序?

回答

0

我有同樣的問題。當我運行我的應用程序而不運行rabbitmq服務器時,我有這些無盡的錯誤消息。 因此,根據您的需求,很少有解決方案。

  1. 如果試圖避免對相同的錯誤無盡消息,您可以配置RecoveryBackOff戰略AbstractRabbitListenerContainerFactory

    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); 
    int maxAttempts = 3; 
    FixedBackOff recoveryBackOff = new FixedBackOff(DEFAULT_INTERVAL, maxAttempts); 
    factory.setRecoveryBackOff(recoveryBackOff); 
    

    這裏,maxAttemps它嘗試重新連接的最大數目。第一個參數是兩次嘗試之間的間隔(DEFAULT_INTERVAL = 5000ms)。

所以,在這種情況下SimpleMessageListenerContainer一樣將嘗試重新連接三次,並會平靜下來。

  • 如果需要使用這些連接失敗,你的貓實施春天ApplicationListenerListenerContainerConsumerFailedEvent,甚至更普遍的做一些事情 - AmqpEventas mentioned here