You can use the receive and receiveNoWait methods to retrieve messages directly instead of waiting for them in the onMessage callback method. These methods work for queues and topics. Both of these methods return an object of type Message, which you must cast to the appropriate message type (such as TextMessage). Use these methods, as follows:
The receive and receiveNoWait methods are defined in the MessageConsumer interface and are available for topics (through TopicSubscriber) and queues (through QueueReceiver).
To use these methods, do not implement the MessageListener interface, code a setMessageListener method, or implement the onMessage method. Instead, after starting the connection, code either receive or receiveNoWait, as the following example shows:
...
topicConnection.start(); ... TextMessage mess = (TextMessage)sub.receiveNoWait(); ...
This technique is particularly useful in servlets and non-MDB EJBs, which cannot implement the MessageListener interface.
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/techniques_jms4.htm