This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Besides the code posted, there is an important paradigm to understand.
A 'receive' will deliver the next message in the destination. So if there are say 'n' messages, you have to wrap the call to receive in a loop. And finally when there is no message in the queue, this will block till the next message arrives. You can of course chose to specify a timeout value after which the receive returns.
If that is not desirable, you can instead make a call to receiveNoWait() which returns null if there is no more messages to be read. By checking for the return value, you can exit the loop.
To receive the message again after a time interval, it is necessary to invoke receive() again.
A MessageListener on the other hand receives messages continously from the destination. The 'looping' is done internally the jms runtime on the client.