I need to implement the following workflow:
- Every N milliseconds fetch all available messages from JMS queue, but not more than K items
- Do some processing
- Acknowledge all of them once processing is done
I'm interested in how to implement step #1 correctly using JMS.
What I've tried is to:
- Create a
BlockingQueue
size K - In the
onMessage
method of my JMSMessageListener
put the messages into theBlockingQueue
. - Every N milliseconds drain the
BlockingQueue
, process the messages, and acknowledge them.
It looks fine at glance, but the issue is that if some message is fetched in onMessage
during processing of the batch then once the batch is done it will be also acked while it is in the BlockingQueue
and was not processed yet (reference). So if my app goes down message will not be processed at all.
What is a better way to implement step #1? Is it something in JMS API or maybe some standard approach?