Scenario - A Queue has message objects. The queue is polled and messages are passed to message handlers. The message retrieval (poll) should be halted on client login event (one or more). I could use a wait/notify mechanism to achive this, but the advice from Joshua Bloch says , there is hardly any reason to use the cumbersome wait/notify mechanism with the advent of concurrent utilities. I am unable to make a choice as to which synchronizers (semaphore, cyclic barrier, countdownlatch) will fit my purpose. Any advice appreciated.
Asked
Active
Viewed 104 times
-1

TheMonkWhoSoldHisCode
- 2,182
- 3
- 26
- 40
-
3This really needs code. What kind of Queue? For example, you could be referring to a [JMS message queue,](http://docs.oracle.com/javaee/6/tutorial/doc/bncdq.html) but I don't think you are. – markspace Nov 03 '14 at 17:51
-
@markspace Messages are pulled from a JMS queue but are then put in to a LinkedList (for async processing). At the moment, the queue I referred to is the LinkedList. – TheMonkWhoSoldHisCode Nov 03 '14 at 17:54
-
JMS is designed for async consumption. Why don't you let the messages in the JMS queue ? – Alexis Hassler Nov 03 '14 at 18:03
-
@AlexisHassler There are many listeners on the JMS queue. Each message goes through every listener. The time each listener takes to handle every message can add up substantially. The point of introducing another queue is to quicken the response time of the listener I am dealing with. – TheMonkWhoSoldHisCode Nov 03 '14 at 18:07
1 Answers
1
If the termination need not be immediate, you could use a "poison pill" pattern. When a user logs in, place a special "termination" object on the Queue. When you poll the Queue, check for that special, unique object. e.g.
public static final Message POISON_PILL = new Message();
...
in your loop {
Message message = queue.take();
if (message == POISON_PILL) // note usage of == here!
stopTheQueue();
else
normalProcessing(message);
}
This means that the Queue will process all Messages that were present before the user logged in. Which may be what you want.
If the stoppage needs to be immediate, check for Thread.interrupted()
As described here. This requires that the login handlers know which Thread is handling the Queue, increasing coupling. So the "poison pill" is nice in that it has less coupling.

Community
- 1
- 1

user949300
- 15,364
- 7
- 35
- 66