Spring amqp: detect shutdown and reconnect to another queue

蹲街弑〆低调 提交于 2019-12-25 08:15:03

问题


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.



来源:https://stackoverflow.com/questions/39203861/spring-amqp-detect-shutdown-and-reconnect-to-another-queue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!