How to pause/resume individual Spring JMS message listeners

依然范特西╮ 提交于 2021-02-05 11:23:21

问题


My Spring Boot JMS application is consuming messages from several SQS queues. Each queue needs to connect to a different external resource in order to process its messages. If there's an external resource failure I expect the consumer requiring that resource will rapidly drain the queue to a DLQ and that's no fun.

I need to be able to suspend (pause) the message listener for each consumer independently when my code detects that its resource is offline (e.g. comms exception calling REST endpoint).

How can I do this?

After searching for platform support for this, I found the AbstractJmsListeningContainer which (via the Lifecycle interface) supports stop() and start() methodsm though it doesn't document whether start can be invoked following a stop call.

My concern is that there appears to be one shared instance among my multiple @JmsListener annotated consumers; so stopping one queue stops them all and I don't want that.

How can I achieve my end goal of pausing individual consumers?

I have seen references to using multiple bean definitions like so:

@Bean
SimpleMessageListenerContainer container1(ConnectionFactory connectionFactory,
        MessageListenerAdapter listenerAdapter) {
// snip
}

@Bean
SimpleMessageListenerContainer container2(ConnectionFactory connectionFactory,
        MessageListenerAdapter listenerAdapter) {
// snip
}

...but never seen any explanation stating how and when one will be used versus the other.


回答1:


See my answer to this question.

Yes, you can call start() after stop().

Note that stop() only stops the threads; the connection remains open.

If you want to shut everything down, call shutDown() after stop() and then initialize() before start().

You should not call stop() on a listener thread, though - hand it off to another thread and wait until isRunning() is false.



来源:https://stackoverflow.com/questions/55836140/how-to-pause-resume-individual-spring-jms-message-listeners

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