How to make Queue browser to stop listening when no more message are in queue?

pruffieux Member Posts: 13 ✭✭

Context: I am using the Java API for browsing messages.

I have my code looking like this:

MessagingService messagingService = createMessagingService(clientUser, clientPass);
Queue queue = Queue.durableExclusiveQueue(queueToBrowse);
MessageQueueBrowser browser;

if (selector == null || selector.isEmpty()) {
browser = messagingService.createMessageQueueBrowserBuilder()
} else {
browser = messagingService.createMessageQueueBrowserBuilder()

if (limit == 0 || limit > 500) {
limit = 500;

for (int msgCount = 0; msgCount < limit; msgCount++) {
InboundMessage message = browser.receiveMessage();"Found a message");

now if the queue is smaller than 500 message, the receiveMessage() (I also tested with a timeout) is never stopping listening indefinitevely the queue for new messages.

How can be sure to get the right number of message?

If I do not use selector, I can easily set the limit to the queue size (calculated before) but with selector, there's no way to find how many message would be returned by the calls.

Note that using the JCSMP API is working well, as there is a getNext() method that will return null if no more message have to be processed but I want to know if this is possible in the new Java API.

The sentence from JCMP API page
"You can continue to use this classic Java API to build new messaging applications, however, if you want to use a Java API that utilizes modern interfaces, patterns, and programming models, consider using the Java API."

makes me feel the JCPMP can be deprecated in the future, hence my question.


Best Answer