<p>This example shows you how to handle a request message and receive a reply. To get a reply message, the requesting client creates a temporary queue. Then it sends out the request message with JMSReplyTo set to the temporary queue. The request message is handled by a SimpleRequestServer, who is listening to the request queue for incoming requests. If a request message has arrived, it extracts the reply queue from the request message by JMSReplyTo header, and sends back a reply message. To let the client know to which request message a reply message is related, the server also set the JMSCorrelationID with the request message's JMSMessageID header to the reply message.</p>
<p>Of course, in a real world example you would re-use the session, producer, consumer and temporary queue and not create a new one for each message!
Or better still use the correlation id, and just store the requests in a map, then you don't need a temporary queue at all
<p>Request/Reply style messaging is supported through standard JMS message headers JMSReplyTo and JMSCorrelationID. This is often used in request-reply style communications between applications.
Whenever a client sends a message that expects a response, it can use this mechanism to implement. please consult the JMS 1.1 specification for full details.</p>
<code>SimpleRequestServer server = new SimpleRequestServer();</code>
<code>server.start();</code>
</pre>
<li>We need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
<preclass="prettyprint">
<code>initialContext = getContext();</code>
</pre>
<li>We lookup the queue for sending the request message</li>
<li>We put the request message to the map. Later we use it to check out which request message a reply message is for. Here we use the MessageID as the correlation id (JMSCorrelationID). You don't have to use it though. You can use some arbitrary string for example.</li>
<li>We check out which request message is this reply message sent for. Here we just have one request message for illustrative purpose. In real world there may be many requests and many replies.</li>
<li>We close the consumer and producer on the replyQueue</li>
<preclass="prettyprint">
<code>replyConsumer.close();</code>
</pre>
<li>We delete the temporary queue</li>
<preclass="prettyprint">
<code>replyQueue.delete();</code>
</pre>
<li>We shutdown the request server</li>
<preclass="prettyprint">
<code>server.shutdown();</code>
</pre>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<preclass="prettyprint">
<code>finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}</code>
</pre>
</ol>
Request Messages are handled in SimpleRequestServer.onMessage(),