activemq-artemis/examples/jms/colocated-failover-scale-down
jbertram e6a3d3a0c5 ACTIVEMQ6-14 Replace JNDI server with client impl 2014-12-05 09:27:52 -06:00
..
src/main ACTIVEMQ6-14 Replace JNDI server with client impl 2014-12-05 09:27:52 -06:00
pom.xml ACTIVEMQ6-14 Replace JNDI server with client impl 2014-12-05 09:27:52 -06:00
readme.html Remove references to HornetQ in doc and Comments 2014-11-19 15:15:35 +00:00

readme.html

<html>
  <head>
    <title>ActiveMQ JMS Colocated Failover Scale Down 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 Recover Only 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 that will scale down.
         That is it will not become live but scale down the journal to the colocated live server.
     <p>This example starts 2 live servers each will request the other to create a backup.</p>
     <p>The first live server will be killed and the backup in the second will recover the journal and recreate its state
         in the live server it shares its VM with.</p>
     <p>The following shows how to configure the backup, the slave is configured <b>&lt;scale-down/></b> which means
         that the backup server will not fully start on fail over, instead it will just recover the journal and write it
     to its parent live server.</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;scale-down/>
                 &lt;/slave>
             &lt;/colocated>
         &lt;/shared-store>
     &lt;/ha-policy>
     </code>
     </pre>
     <p>Notice that we dont need to specify a scale down connector as it will use most appropriate
     from the list of available connectors which in  this case is the first INVM connector</p>
     <p> One other thing to notice is that the cluster connection has its reconnect attempts set to 5, this is so it will
         disconnect instead of trying to reconnect to a backup that doesn't exist.</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> 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, notice that they will be out of order, this is
             because they were initially round robined to both nodes then when the server failed were reloaded into the
             live server.</li>
        <pre class="prettyprint">
           <code>
    TextMessage message0 = null;
    for (int i = 0; i < numMessages * 2; i++)
    {
       message0 = (TextMessage)consumer.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>