A queue peek adapter for JMS
Introduction
I’ve been working with some Java technologies of late, including tools like JMS.
Coming from a .Net world, JMS is very similar in concept to a technology like WCF. It provides an abstract way of working with message queues.
One thing which I found a little frustrating for development testing purposes was the lack of a QueueReceiver
which acts in a peek function as opposed to pulling the actual message.
This is useful because sometimes you can’t control adding new messages to the queue, and so developing against a peek message means you can recyle your messages with very little additional code.
The Code
So without further babble, I wrote an adapter which adapts the JMS QueueBrowser
to a QueueReciever
.
Consuming messages in this may is then as simple as:
where destination
is obvisouly the queue that you are monitoring. Note that the destination must be a Queue descendant, and not a topic.
My Adapter class implmentation is pretty much a wrapper that peeks the first message every time. It wouldn’t be too difficult to make it peek all messages though.
Conclusion
So, above is an adaption of the QueueBrowser
to a QueueReceiver
which makes recycling messages from JMS almsot trivial.