<p>This example shows you how to use a <ahref="http://java.sun.com/javaee/5/docs/api/javax/jms/QueueRequestor.html">QueueRequestor</a> with ActiveMQ.</p>
<p>JMS is mainly used to send messages asynchronously so that the producer of a message is not waiting for the result of the message consumption.
However, there are cases where it is necessary to have a synchronous behavior: the code sending a message requires a reply for this message
before continuing its execution.<br/>
A QueueRequestor facilitates this use case by providing a simple request/reply abstraction on top of JMS.</p>
<p>The example consists in two classes:</p>
<dl>
<dt><code>TextReverserService</code></dt>
<dd>A JMS MessageListener which consumes text messages and sends replies containing the reversed text</dd>
<dt><code>QueueRequestorExample</code></dt>
<dd>A JMS Client which uses a QueueRequestor to send text requests to a queue and receive replies with the reversed text in a synchronous fashion</dd>
</dl>
<h2>Example step-by-step</h2>
<p><i>To run the example, simply type <code>mvn verify</code> from this directory</i></p>
<ol>
<li>First we need to get an initial context so we can look-up the JMS connection factory 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>
<li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
<preclass="prettyprint">
<code>connection.start();</code>
</pre>
<li>We create a JMS queue session. The session is created as non transacted and will auto acknowledge messages (this is mandatory to use it to create a QueueRequestor)</li>
<li>We close the queue requestor to release all the JMS resources it created to provide request/reply mechanism</li>
<preclass="prettyprint">
<code>queueRequestor.close()</code>
</pre>
<li>We do the same for the text reverser service</li>
<preclass="prettyprint">
<code>reverserService.close()</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>