PDA

View Full Version : Draining a JMS queue using Spring 1.2.6



sliebeskind
Dec 10th, 2005, 04:38 PM
Is there some way to use Spring 1.2.6 JMS classes to drain a JMS queue of messages?

I'm relatively new to JMS programming, but I do know there is a method called receiveNoWait on the interface javax.jms.MessageConsumer, which would pull off any waiting message, or return a null message if nothing is waiting. That's the functionality I am looking for. The logical place to me, the novice, to find this functionality would be in the JmsTemplate class (a method called JmsTemplate.receiveNoWait?), but JmsTemplate receive methods end up triggering calls only to MessageConsumer.receive() and MessageConsumer.receive(<timeout value>) , there's no reference to MessageConsumer.receiveNoWait.

I can fake receiveNoWait, I guess by calling receive with at tiny timeout value, but is that the right way to go? Is there some other way using Spring (perhaps not involving the JMSTemplate at all) if I want to drain a given queue of all messages?

Thanks.

Susan Liebeskind

jstrachan
Dec 12th, 2005, 03:55 AM
I'd use receive(timeout) with some timeout thats fairly small like 2000. If you have lots of messages available it'll return pretty quickly anyway.

Not all JMS providers return immediately with a message if you call receiveNoWait() and there may be messages on the broker, so its worth waiting a second or so just to be sure the queue really is drained.