简体   繁体   English

一次获取队列中的所有消息(Spring Boot JMSListerner 注释)

[英]Get all messages in queue at once (Spring Boot JMSListerner annotation)

I have a peculiar requirement to get all the pending messages on a queue for processing them further.我有一个特殊的要求,即获取队列中的所有待处理消息以进一步处理它们。 To elaborate.详细说明。 this is what is going to be the flow of the application:这就是应用程序的流程:

  • There is a queue setup that will host messages有一个队列设置将托管消息
  • The listener to this queue is stopped by default by using MessageListenerContainer 's stop() method.默认情况下,使用MessageListenerContainerstop()方法停止此队列的侦听器。
  • The messages on the queue will be listened to on demand by calling listenerContainer.start()队列中的消息将通过调用listenerContainer.start()按需listenerContainer.start()
  • The requirement is that I should get all the messages that are available on the queue at once so only those are to be processed.要求是我应该立即获取队列中可用的所有消息,以便只处理这些消息。 If for any reason, new messages are posted on the queue during processing of existing messages, then those should remain on the queue itself.如果出于任何原因,在处理现有消息期间将新消息发布到队列中,则这些消息应保留在队列本身中。 Those will be read later once again when listenerContainer.start();稍后会在listenerContainer.start();时再次读取这些内容listenerContainer.start(); is invoked on demand.按需调用。

For example:例如:

  • Let's say we have a queue named abc-queue .假设我们有一个名为abc-queue There's a JMS listener configured as:有一个 JMS 侦听器配置为:

     @JmsListener(destination = "abc-queue", containerFactory = "abcFactory", selector = "_type='java.util.LinkedHashMap'", id = "abc-listener")
  • On app startup, we will do this:在应用程序启动时,我们将执行以下操作:

     MessageListenerContainer listenerContainer = jmsListenerEndpointRegistry.getListenerContainer("abc-listener"); listenerContainer.stop();
  • App will have a controller endpoint to listen to this queue on demand.应用程序将有一个控制器端点来按需侦听此队列。 Once that's called, this will happen:一旦被调用,这将发生:

     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

Questions:问题:

  • How do we read all messages at once to store them in a list?我们如何一次读取所有消息以将它们存储在列表中?
  • Even if there are messages present on the queue, listenerContainer.start() doesn't start processing the queue immediately.即使队列中存在消息, listenerContainer.start()也不会立即开始处理队列。 It's triggered only after another message appears on the queue.只有在队列中出现另一条消息后才会触发它。

Any useful pointers are appreciated.任何有用的指针表示赞赏。

Potential solutions:可能的解决方案:

  • Like Daniel mentioned in the comment, moving the messages to another queue.就像 Daniel 在评论中提到的那样,将消息移动到另一个队列。
  • Moving the messages from queue to a file.将消息从队列移动到文件。
  • Moving the messages from queue to database.将消息从队列移动到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM