質問

One of the nice features of Spring-AMQP is that it appears to implement the required logic to reconnect Spring application to the message broker when the broker goes down and comes back up.

What I'm looking for is the preferred method of detecting when such disconnect/reconnect events occur within the application.

The best I've come up with so far is a polling-like mechanism where I attempt to acquire a channel from the connection factory at a regular interval.

Thanks in advance!

役に立ちましたか?

解決

Feel free to open an 'Improvement' JIRA Issue - we could emit an ApplicationEvent when the connection is lost (and re-established); you could then subscribe to that event with an ApplicationListener.

In the meantime, you could do something via your logging subsystem - for example, with Log4J, you can create a custom appender to get logs with the SimpleMessageListenerContainer category, such as...

16:05:50.042 WARN  [SimpleAsyncTaskExecutor-6][org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: com.rabbitmq.client.ShutdownSignalException: connection error; reason: {#method<connection.close>(reply-code=320, reply-text=CONNECTION_FORCED - broker forced connection closure with reason 'shutdown', class-id=0, method-id=0), null, ""}

16:05:55.048 WARN  [SimpleAsyncTaskExecutor-7][org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer] Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused

EDIT:

There is also a ConnectionListener interface which you can register an implementation of with the connection factory; you can learn of new connections with it, but onClose() is currently only called when the connection is explicitly closed by destroying the factory.

However, the onCreate() gives you a handle to the connection so you could call its isOpen() method from time to time, but don't do that in the onCreate() method itself - save off the connection. You must exit the onCreate() for all to work as planned.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top