问题描述
我的 Spring Boot JMS 应用程序正在使用来自多个 SQS 队列的消息.每个队列都需要连接到不同的外部资源以处理其消息.如果出现外部资源故障,我预计需要该资源的使用者会迅速将队列排空到 DLQ,这没什么意思.
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.
当我的代码检测到其资源离线(例如,调用 REST 端点的通信异常)时,我需要能够独立地挂起(暂停)每个使用者的消息侦听器.
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).
我该怎么做?
在为此搜索平台支持后,我找到了 AbstractJmsListeningContainer,它(通过 Lifecycle 接口)支持 stop() 和 start() 方法,尽管它没有记录是否可以在停止调用后调用 start.
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.
我担心的是,我的多个@JmsListener 注释消费者之间似乎有一个共享实例;所以停止一个队列会停止所有队列,我不希望那样.
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?
我已经看到使用多个 bean 定义的参考,如下所示:
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.
推荐答案
查看我对 这个问题.
是的,您可以在 stop()
之后调用 start()
.
Yes, you can call start()
after stop()
.
注意stop()
只停止线程;连接保持打开状态.
Note that stop()
only stops the threads; the connection remains open.
如果你想关闭一切,在stop()
之后调用shutDown()
,然后在start之前调用
.initialize()
()
If you want to shut everything down, call shutDown()
after stop()
and then initialize()
before start()
.
您不应该在侦听器线程上调用 stop()
,但是 - 将其交给另一个线程并等待 isRunning()
为 false.
You should not call stop()
on a listener thread, though - hand it off to another thread and wait until isRunning()
is false.
这篇关于如何暂停/恢复单个 Spring JMS 消息侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!