When Bobby and I worked on the simple JMS Request/Reply Example, we ran into a simple, but interesting problem. The example consists of a Requestor which sends a message to a Replier and waits for the response. The example uses two Point-to-Point Channels, RequestQueue and ReplyQueue (see picture).
We had plastered println statements all over the code so we can see what is going on. We started the Replier first, then the Requestor. Then a very odd thing happened. The Requestor console window claimed to have gotten a response before the Replier ever acknowledged receiving a request. A delay in the console output? Lacking any great ideas, we decided to shut the Replier down and re-ran the Requestor. Odd enough, we still received a response to our request! Magic? No, just a side effect of persistent messaging. A superfluous message was present on the ReplyQueue. When we started the Requestor it placed a new message on the RequestQueue and then immediately retrieved the extraneous reply message that was sitting on the ReplyQueue. We never noticed that this was not the reply to the request the Requestor just made! Once the Replier received the request message, it placed a new reply message on the ReplyQueue so that the 'magic' repeated during the next test. It can be amazing (or amazingly frustrating) how persistent, asynchronous messaging can play tricks on you in even the most simple scenarios!
How can you keep 'left-over' messages on a channel from disturbing tests or running systems?
Use a Channel Purger to remove unwanted messages from a channel.
A simple Channel Purger simply remove all the messages from a channel. This may be sufficient for test scenarios where we want to reset the system into a consistent state. If we are debugging a production system we may need to remove individual message or a set of messages based on specific criteria, such as the message ID or the values of specific message fields.
... Read the entire pattern in the book Enterprise Integration Patterns
Example: Channel Purger in Amazon SQSNEW
Many hosted messaging systems have a built-in function to purge channels. For example, with Amazon's Simple Queuing Service (SQS), you can purge a queue simply from the web user interface:
Related patterns:
Correlation Identifier, Invalid Message Channel, Message Channel, Message Store, Point-to-Point Channel, JMS Request/Reply Example