activemq-artemis/examples/jms/colocated-failover
jbertram 20326d0d83 ARTEMIS-19 allow disabling of message load-balancing 2015-06-03 16:21:43 -05:00
..
src/main ARTEMIS-19 allow disabling of message load-balancing 2015-06-03 16:21:43 -05:00
pom.xml [maven-release-plugin] prepare for next development iteration 2015-05-21 13:47:45 +01:00
readme.html Ensure all references to the project use ActiveMQ Artemis 2015-05-13 11:51:26 +01: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 Artemis JMS Colocated Failover 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 Colocated Failover Shared Store Example</h1>

     <p>This example demonstrates how you can colocate live and backup servers in the same VM. We do this by creating an
     HA Policy that is colocated. colocated means that backup servers can be created and maintained by live servers on behalf
     of other requesting live servers. In this example we create a colocated shared store server.
     <p>This example starts 2 live servers each with a backup server that backs up the other live server.</p>
     <p>The first live server will be killed and the backup in the second will become live</p>
     <p>The following shows how to configure the live servers to request and allow backups to be deployed</p>
     <pre class="prettyprint">
     <code>&lt;ha-policy>
         &lt;shared-store>
             &lt;colocated>
                 &lt;backup-port-offset>100&lt;/backup-port-offset>
                 &lt;backup-request-retries>-1&lt;/backup-request-retries>
                 &lt;backup-request-retry-interval>2000&lt;/backup-request-retry-interval>
                 &lt;max-backups>1&lt;/max-backups>
                 &lt;request-backup>true&lt;/request-backup>
                 &lt;master/>
                 &lt;slave/>
             &lt;/colocated>
         &lt;/shared-store>
     &lt;/ha-policy>
     </code>
     </pre>
     <p>notice that we have used a template to set some sensible defaults but overridden the backup strategy so back ups
     are full servers</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 for both servers</li>
        <pre class="prettyprint">
           <code>
    initialContext1 = getContext(1);
    initialContext = getContext(0);
   </code>
        </pre>

        <li>Look up the JMS resources from JNDI</li>
        <pre class="prettyprint">
           <code>
    Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
    ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
    ConnectionFactory connectionFactory1 = (ConnectionFactory)initialContext1.lookup("/ConnectionFactory");
           </code>
        </pre>

        <li>Create a JMS Connections</li>
        <pre class="prettyprint">
           <code>
    connection = connectionFactory.createConnection();
    connection1 = connectionFactory1.createConnection();
           </code>
        </pre>

        <li>Create a *non-transacted* JMS Session with client acknowledgement</li>
        <pre class="prettyprint">
           <code>
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    Session session1 = connection1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
           </code>
        </pre>

        <li>Create a JMS MessageProducer</li>
        <pre class="prettyprint">
           <code>
    MessageProducer producer = session.createProducer(queue);
    MessageProducer producer1 = session1.createProducer(queue);
           </code>
        </pre>

        <li>Send some messages to both servers</li>
        <pre class="prettyprint">
          <code>
    for (int i = 0; i < numMessages; i++)
    {
      TextMessage message = session.createTextMessage("This is text message " + i);
      producer.send(message);
      System.out.println("Sent message: " + message.getText());
      message = session1.createTextMessage("This is another text message " + i);
      producer1.send(message);
      System.out.println("Sent message: " + message.getText());
    }
          </code>
        </pre>
        
        <li>Crash server #0, the live server</li>
        <pre class="prettyprint">
          <code>
    killServer(0);
          </code>
        </pre>

        <li>start the connection ready to receive messages</li>
        <pre class="prettyprint">
           <code>
    connection1.start();
           </code>
        </pre>
        
        <li>create a consumer</li>
        <pre class="prettyprint">
           <code>
    MessageConsumer consumer = session1.createConsumer(queue);
            </code>
        </pre>

        <li>Receive and acknowledge all of the sent messages, the backup server that is colocated with server 1
            will have become live and is now handling messages for server 0.</li>
        <pre class="prettyprint">
           <code>
    TextMessage message0 = null;
    for (int i = 0; i < numMessages; i++)
    {
       message0 = (TextMessage)consumer.receive(5000);
       System.out.println("Got message: " + message0.getText());
    }
    message0.acknowledge();
           </code>
        </pre>

         <li>Receive and acknowledge the rest of the sent messages from server 1.</li>
        <pre class="prettyprint">
           <code>
   for (int i = 0; i < numMessages; i++)
   {
   message0 = (TextMessage)consumer1.receive(5000);
   System.out.println("Got message: " + message0.getText());
   }
   message0.acknowledge();
           </code>
        </pre>

        </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 (connection != null)
       {
       connection.close();
       }

       if (initialContext != null)
       {
       initialContext.close();
       }
       if (connection1 != null)
       {
       connection1.close();
       }

       if (initialContext1 != null)
       {
       initialContext1.close();
       }
    }
           </code>
        </pre>

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