立即获取队列中的所有消息(Spring Boot JMSListerner 注释)
Get all messages in queue at once (Spring Boot JMSListerner annotation)
我有一个特殊的要求,要将所有待处理的消息放入队列中以便进一步处理它们。详细说明。这就是应用程序的流程:
- 有一个将托管消息的队列设置
- 默认情况下,使用
MessageListenerContainer
的 stop()
方法停止此队列的侦听器。
- 队列中的消息将通过调用
listenerContainer.start()
按需收听
- 要求是我应该立即获取队列中可用的所有消息,以便只处理那些消息。如果出于任何原因,在处理现有消息期间将新消息发布到队列中,那么这些消息应该保留在队列本身中。这些将在稍后按需调用
listenerContainer.start();
时再次阅读。
例如:
假设我们有一个名为 abc-queue
的队列。有一个 JMS 侦听器配置为:
@JmsListener(destination = "abc-queue",
containerFactory = "abcFactory",
selector = "_type='java.util.LinkedHashMap'",
id = "abc-listener")
在应用程序启动时,我们将这样做:
MessageListenerContainer listenerContainer = jmsListenerEndpointRegistry.getListenerContainer("abc-listener");
listenerContainer.stop();
应用程序将有一个控制器端点来按需侦听此队列。一旦被调用,这将发生:
MessageListenerContainer listenerContainer = jmsListenerEndpointRegistry.getListenerContainer("abc-listener");
listenerContainer.start();
// now read all the messages in queue at once and store them in a list
// listenerContainer.stop();
// start processing the messages one by one
问题:
- 我们如何一次读取所有消息并将它们存储在列表中?
- 即使队列中存在消息,
listenerContainer.start()
也不会立即开始处理队列。它仅在另一条消息出现在队列中后触发。
感谢任何有用的指点。
可能的解决方案:
- 就像评论中提到的丹尼尔,将消息移到另一个队列。
- 正在将消息从队列移动到文件。
- 将消息从队列移动到数据库。
我有一个特殊的要求,要将所有待处理的消息放入队列中以便进一步处理它们。详细说明。这就是应用程序的流程:
- 有一个将托管消息的队列设置
- 默认情况下,使用
MessageListenerContainer
的stop()
方法停止此队列的侦听器。 - 队列中的消息将通过调用
listenerContainer.start()
按需收听
- 要求是我应该立即获取队列中可用的所有消息,以便只处理那些消息。如果出于任何原因,在处理现有消息期间将新消息发布到队列中,那么这些消息应该保留在队列本身中。这些将在稍后按需调用
listenerContainer.start();
时再次阅读。
例如:
假设我们有一个名为
abc-queue
的队列。有一个 JMS 侦听器配置为:@JmsListener(destination = "abc-queue", containerFactory = "abcFactory", selector = "_type='java.util.LinkedHashMap'", id = "abc-listener")
在应用程序启动时,我们将这样做:
MessageListenerContainer listenerContainer = jmsListenerEndpointRegistry.getListenerContainer("abc-listener"); listenerContainer.stop();
应用程序将有一个控制器端点来按需侦听此队列。一旦被调用,这将发生:
MessageListenerContainer listenerContainer = jmsListenerEndpointRegistry.getListenerContainer("abc-listener"); listenerContainer.start(); // now read all the messages in queue at once and store them in a list // listenerContainer.stop(); // start processing the messages one by one
问题:
- 我们如何一次读取所有消息并将它们存储在列表中?
- 即使队列中存在消息,
listenerContainer.start()
也不会立即开始处理队列。它仅在另一条消息出现在队列中后触发。
感谢任何有用的指点。
可能的解决方案:
- 就像评论中提到的丹尼尔,将消息移到另一个队列。
- 正在将消息从队列移动到文件。
- 将消息从队列移动到数据库。