Issue
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
'sstop()
method. - The messages on the queue will be listened to on demand by calling
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();
is invoked on demand.
For example:
Let's say we have a queue named
abc-queue
. There's a JMS listener configured as:@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. It's triggered only after another message appears on the queue.
Any useful pointers are appreciated.
Solution
Potential solutions:
- Like Daniel mentioned in the comment, moving the messages to another queue.
- Moving the messages from queue to a file.
- Moving the messages from queue to database.
Answered By - Pramod Karandikar
Answer Checked By - Willingham (JavaFixing Volunteer)