Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Not A Problem
-
None
-
None
Description
According to the documentation I've read and response to message posts, I've concluded that a call to consumer.receive(long) must guard against another thread using the session the consumer was created with.
This makes it difficult to write performant code because a busy-wait idiom must be used in order to not hold a lock on session access while waiting for new messages. I've implemented a new method that will block until messages are available but does not require a session lock (it does not return any messages):
/** * Waits until messages are available. Unless the caller is interrupted, there will be a message available when this * method returns. */ public void waitUntilMessagesAvailable() { while (unconsumedMessages.isEmpty()) { synchronized (unconsumedMessages.getMutex()) { try { unconsumedMessages.getMutex().wait(2000); // the dispatch channel notifies when a message arrives } catch (InterruptedException e) { break; } } } }
I'll be happy to submit as a patch with associated unit test if there is consensus that this is needed and useful.