<p>Durable subscriptions are a standard part of JMS, please consult the JMS 1.1 specification for full details.</p>
<p>Unlike non durable subscriptions, the key function of durable subscriptions is that the messages contained in them
persist longer than the lifetime of the subscriber - i.e. they will accumulate messages sent to the topic even
if the subscriber is not currently connected. They will also survive server restarts. Note that for the messages to
be persisted, the messages sent to them must be marked as persistent messages.</p>
<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 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 look-up the JMS topic object from JNDI</li>
<li>We set the client-id on the connection. This must be the <b>first operation</b> performed on the connection object.
The combination of client-id and durable subscription name uniquely identifies the durable subscription. Maybe different durable subscritions can have the same name if they belong to different client-id values</li>
<li>We create the durable subscriber on the topic, specifying it's name. Since this is the first time the subscriber is created and a subscription with that name and for this client-id does not already exist, then the underlying durable subscription will be created, and a subscriber will be created and returned for that subscription.</li>
<li>We create a JMS text message, message 1, that we are going to send. Note that it must be a persistent message in order to survive server restart.</li>
<preclass="prettyprint">
<code>TextMessage message1 = session.createTextMessage("This is a text message 1");</code>
</pre>
<li>We send message 1 to the topic</li>
<preclass="prettyprint">
<code>messageProducer.send(message1);</code>
</pre>
<li>The message arrives in the subscription, and we consume the message from the subscription.</li>
<li>We create and send another text message, message 2, to the same topic</li>
<preclass="prettyprint">
<code>TextMessage message2 = session.createTextMessage("This is a text message 2");
messageProducer.send(message2);</code>
</pre>
<li>Now we close the subscriber. Since the subscription is durable it will continue to survive even though there is no subscriber attached to it. At this point you could even stop and restart the server and the subscription would survive!</li>
<preclass="prettyprint">
<code>subscriber.close();</code>
</pre>
<li>We now create another durable subscriber, with the same name and same client-id on the same topic. Since the durable subscrition already exists, it will simply return a new subscriber consuming from the <i>same</i> durable subscription instance as before</li>
<li>Now we <i>delete</i> the underlying durable subscription. This will delete any remaining unacknowledged messages in the subscription and a new subscriber will not be able to access them</li>
<preclass="prettyprint">
<code>session.unsubscribe("subscriber-1");</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>