O'Reilly - Java Message Service
O'Reilly - Java Message Service
O'Reilly - Java Message Service
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
The receive( ) method with no parameters blocks indefinitely, until a message is<br />
received. The receive(long timeout) method blocks until a message is received, or until<br />
the timeout period expires, whichever comes first. The receive( ) method will return null<br />
if the session is closed while the method is blocking. The receiveNoWait( ) method does<br />
not block at all. It either returns a message if one is available, or it returns null, if there is<br />
nothing currently pending to be delivered. Here is a slightly modified version of<br />
Wholesaler.publishPriceQuotes() that makes use of the receive( ) method:<br />
private void publishPriceQuotes(String dealDesc, String username,<br />
String itemDesc, float oldPrice,<br />
float newPrice){<br />
...<br />
System.out.println("\nInitiating Synchronous Request");<br />
// Publish the message persistently<br />
publisher.publish(<br />
msg, //message<br />
javax.jms.DeliveryMode.PERSISTENT, //publish persistently<br />
javax.jms.<strong>Message</strong>.DEFAULT_PRIORITY,//priority<br />
MESSAGE_LIFESPAN); //Time to Live<br />
javax.jms.<strong>Message</strong> a<strong>Message</strong> = subscriber.receive( );<br />
System.out.println("\nRequest Sent, Reply Received!");<br />
if (a<strong>Message</strong> != null)<br />
{<br />
on<strong>Message</strong>(a<strong>Message</strong>);<br />
}<br />
...<br />
}<br />
In this example the subscriber, which subscribes to the "Buy Order" temporary topic, has<br />
its receive( ) method called. The receive( ) method blocks until a message is published<br />
by the Retailer to the "Buy Order" topic. The Wholesaler client becomes a synchronous<br />
client waiting for the Retailer to respond. When the receive( ) method returns with a<br />
message, the Wholesaler simply calls on<strong>Message</strong>( ) directly to process the message.<br />
Due to threading restrictions imposed on a JMS session object, it is impractical to have<br />
both synchronous and asynchronous operations on a session. Hence the Wholesaler's<br />
constructor does not make a call to set<strong>Message</strong>Listener(this). The on<strong>Message</strong>( ) handler<br />
will never get called automatically.<br />
The recipient side of the conversation still looks the same as in our previous example. The<br />
Retailer.autoBuy( ) method receives the message, gets the return address from the<br />
JMSReplyTo header, and publishes a response using that topic.<br />
It is erroneous for a session to be operated by more than one thread of control at any given<br />
time. In our example, there appears to be only one thread of control: the main thread of the<br />
application. However, when the on<strong>Message</strong>( ) handler is invoked, it is being called by<br />
another thread that is owned by the JMS provider. Due to the asynchronous nature of the<br />
on<strong>Message</strong>( ) callback, it could possibly be invoked while the main thread is blocking on a<br />
synchronous receive( ) .<br />
66