Spring amqp: detect shutdown and reconnect to anot

2019-09-16 12:46发布

问题:

We have this setup where we call a webservice to create a queue, and receive the queue name from the response.

Then we set up a SimpleMessageListenerContainer and set the queue name there, and then start it.

However, from time to time, the queue is deleted - resulting in a "404 could not declare queue XXXXXXXXX" error. In these cases, I need to call the webservice again and add the new QueueName to the SimpleMessageListenerContainer and then removing the old one.

The only way I figured out trigger any code to handle this was to create a custom CachedConnectionFactory and overriding the shutdownCompleted method.

However, shutdownCompleted seems to trigger when the SimpleMessageListenerContainer switches over as well, so it sticks in a loop. The ShutdownSignalException sent into shutdownCompleted does not seem to look any different if the trigger is external from the server or from the client handling the new queue, so I can't figure out how to skip the handling on the "second" go.

So what is the usual way to detect and run custom handling when the server kills the queue?

回答1:

The container publishes a ListenerContainerConsumerFailedEvent when the listener fails.

Add an ApplicationListener<ListenerContainerConsumerFailedEvent>, stop the container, change the queues and restart.

You will likely get multiple events because, by default, the container will try to reconnect 3 times before giving up and stopping itself.



标签: spring-amqp