activemq-artemis/examples/jms/clustered-durable-subscription
jbertram 88ed03e2bd ACTIVEMQ6-87 Strip @author tags 2015-03-10 15:23:07 -05:00
..
src/main ACTIVEMQ6-87 Strip @author tags 2015-03-10 15:23:07 -05:00
pom.xml [maven-release-plugin] prepare for next development iteration 2015-03-10 13:25:56 +00:00
readme.html added examples to release profile 2015-03-08 13:34:02 +00:00

readme.html

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<html>
  <head>
    <title>ActiveMQ JMS Durable Subscription Example</title>
    <link rel="stylesheet" type="text/css" href="../common/common.css" />
    <link rel="stylesheet" type="text/css" href="../common/prettify.css" />
    <script type="text/javascript" src="../common/prettify.js"></script>
  </head>
  <body onload="prettyPrint()">
     <h1>JMS Durable Subscription Example</h1>

     <p>This example demonstrates a clustered JMS durable subscription.
     Normally durable subscriptions exist on a single node and can only have one subscriber at any one time,
     however, with ActiveMQ it's possible to create durable subscription instances with the same name and client-id
     on different nodes of the cluster, and consume from them simultaneously.
     This allows the work of processing messages from a durable subscription to be spread across the cluster in
     a similar way to how JMS Queues can be load balanced across the cluster
     </p>     
     <p>In this example we first configure the two nodes to form a cluster, then we then create a durable subscriber 
     with the same name and client-id on both nodes, and we create a producer on only one of the nodes.</p>
     <p>We then send some messages via the producer, and we verify that the messages are round robin'd between
     the two subscription instances. Note that each durable subscription instance with the same name and client-id 
     <b>does not</b> receive its own copy of the messages. This is because the instances on different nodes form a
     single "logical" durable subscription, in the same way multiple JMS Queue instances on different nodes
     form a single "local" JMS Queue</p>     
     <p>This example uses JNDI to lookup the JMS Queue and ConnectionFactory objects. If you prefer not to use
     JNDI, these could be instantiated directly.     
     <p>Here's the relevant snippet from the server configuration, which tells the server to form a cluster between the two nodes
     and to load balance the messages between the nodes.</p>     
     <pre class="prettyprint">
     <code>&lt;cluster-connection name="my-cluster"&gt;
        &lt;address&gt;jms&lt;/address&gt;
        &lt;retry-interval&gt;500&lt;/retry-interval&gt;
        &lt;use-duplicate-detection&gt;true&lt;/use-duplicate-detection&gt;
        &lt;forward-when-no-consumers&gt;true&lt;/forward-when-no-consumers&gt;
        &lt;max-hops&gt;1&lt;/max-hops&gt;
        &lt;discovery-group-ref discovery-group-name="my-discovery-group"/&gt;
     &lt;/cluster-connection&gt;
     </code>
     </pre>    
     <p>For more information on ActiveMQ load balancing, and clustering in general, please see the clustering
     section of the user manual.</p>    
       
     <h2>Example step-by-step</h2>
     <p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i></p>

     <ol>
        <li> Get an initial context for looking up JNDI from server 0.</li>
        <pre class="prettyprint">
           <code>
   ic0 = getContext(0);
   </code>
        </pre>

        <li>Look-up the JMS Topic object from JNDI</li>
        <pre class="prettyprint">
           <code>Topic topic = (Topic)ic0.lookup("/topic/exampleTopic");</code>
        </pre>

        <li>Look-up a JMS Connection Factory object from JNDI on server 0</li>
        <pre class="prettyprint">
           <code>ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");</code>
        </pre>

        <li>Get an initial context for looking up JNDI from server 1.</li>
        <pre class="prettyprint">
           <code>ic1 = getContext(1);</code>
        </pre>

        <li>Look-up a JMS Connection Factory object from JNDI on server 1</li>
        <pre class="prettyprint">
           <code>ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
           </code>
        </pre>

        <li>We create a JMS Connection connection0 which is a connection to server 0
        and set the same client-id.</li>
        <pre class="prettyprint">
          <code>
   connection0 = cf0.createConnection();
   final String clientID = "my-client-id";         
   connection0.setClientID(clientID);
          </code>
        </pre>
        
        <li>We create a JMS Connection connection1 which is a connection to server 1
        and set the same client-id.</li>
        <pre class="prettyprint">
          <code>
   connection1 = cf1.createConnection();
   connection1.setClientID(clientID);
          </code>
        </pre>

        <li>We create a JMS Session on server 0</li>
        <pre class="prettyprint">
           <code>
   Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
           </code>
        </pre>
        
        <li>We create a JMS Session on server 1</li>
        <pre class="prettyprint">
           <code>
   Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
            </code>
        </pre>

        <li>We start the connections to ensure delivery occurs on them</li>
        <pre class="prettyprint">
           <code>
   connection0.start();

   connection1.start();
           </code>
        </pre>

        <li>We create JMS durable subscriptions with the same name and client-id on both nodes
        of the cluster
        </li>
        <pre class="prettyprint">
           <code>
   final String subscriptionName = "my-subscription";
         
   MessageConsumer subscriber0 = session0.createDurableSubscriber(topic, subscriptionName);

   MessageConsumer subscriber1 = session1.createDurableSubscriber(topic, subscriptionName);
           </code>
        </pre>

        <li>We create a JMS MessageProducer object on server 0.</li>
        <pre class="prettyprint">
           <code>
   MessageProducer producer = session0.createProducer(topic);</code>
        </pre>

        <li>We send some messages to server 0.</li>
        <pre class="prettyprint">
           <code>
	final int numMessages = 10;

	for (int i = 0; i < numMessages; i++)
	{
	   TextMessage message = session0.createTextMessage("This is text message " + i);
	      
	   producer.send(message);
	
	   System.out.println("Sent message: " + message.getText());
	}
           </code>
        </pre>
        
        <li>
        We now consume those messages on *both* server 0 and server 1.
        Note that the messages have been load-balanced between the two nodes, with some
        messages on node 0 and others on node 1.
        The "logical" subscription is distributed across the cluster an contains exactly one copy of all the messages sent.
         </li>
        <pre class="prettyprint">
           <code>
	for (int i = 0; i < numMessages; i += 2)
	{
	   TextMessage message0 = (TextMessage)consumer0.receive(5000);
	
	   System.out.println("Got message: " + message0.getText() + " from node 0");
	
	   TextMessage message1 = (TextMessage)consumer1.receive(5000);
	
	   System.out.println("Got message: " + message1.getText() + " from node 1");
	}
           </code>
        </pre> 

        <li>And finally (no pun intended), <b>always</b> remember to close your JMS 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>

        <pre class="prettyprint">
           <code>
	finally
	{
	   if (connection0 != null)
	   {
	      connection0.close();
	   }
	      
	   if (connection1 != null)
	   {
	      connection1.close();
	   }
	}
           </code>
        </pre>

     </ol>
  </body>
</html>