JMS Failover Without Transactions Example

This example demonstrates two servers coupled as a live-backup pair for high availability (HA), and a client connection failing over from live to backup when the live server is crashed.

Failover behavior differs whether the JMS session is transacted or not.

When a non-transacted JMS session is used, once and only once delivery is not guaranteed and it is possible some messages will be lost or delivered twice, depending when the failover to the backup server occurs.

It is up to the client to deal with such cases. To ensure once and only once delivery, the client must use transacted JMS sessions (as shown in the example for failover with transactions).

For more information on ActiveMQ failover and HA, and clustering in general, please see the clustering section of the user manual.

Example step-by-step

To run the example, simply type mvn verify from this directory

In this example, the live server is server 1, and the backup server is server 0

The connection will initially be created to server1, server 1 will crash, and the client will carry on seamlessly on server 0, the backup server.

  1. Get an initial context for looking up JNDI from server #1.
  2.            initialContext = getContext(1);
            
  3. Look up the JMS resources from JNDI on server #1.
  4.            Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
               ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
            
  5. Create a JMS Connection
  6.            connection = connectionFactory.createConnection();
            
  7. Create a JMS non-transacted Session with client acknowledgement
  8.            Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            
  9. Start the connection to ensure delivery occurs
  10.            connection.start();
            
  11. Create a JMS MessageProducer and MessageConsumer
  12.            MessageProducer producer = session.createProducer(queue);
               MessageConsumer consumer = session.createConsumer(queue);
            
  13. Send some messages to server #1
  14.            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());
               }
            
  15. Receive and acknowledge half of the sent messages
  16.            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();
            
  17. Receive the second half of the sent messages but do not acknowledge them yet
  18.            for (int i = numMessages / 2; i < numMessages; i++)
               {
                  message0 = (TextMessage)consumer.receive(5000);
                  System.out.println("Got message: " + message0.getText());
               }
            
  19. Crash server #1, the live server, and wait a little while to make sure it has really crashed.
  20.            killServer(1);
               Thread.sleep(2000);
            
  21. Acknowledging the second half of the sent messages will fail as failover to the backup server has occurred
  22.            try
               {
                  message0.acknowledge();
               }
               catch (JMSException e)
               {
                  System.err.println("Got exception while acknowledging message: " + e.getMessage());
               }
            
  23. Consume again the second half of the messages againg. Note that they are not considered as redelivered
  24.            for (int i = numMessages / 2; i < numMessages; i++)
               {
                  message0 = (TextMessage)consumer.receive(5000);
                  System.out.printf("Got message: %s (redelivered?: %s)\n", message0.getText(), message0.getJMSRedelivered());
               }
               message0.acknowledge();
            
  25. And finally, always remember to close your resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  26.            finally
               {
                  if (connection != null)
                  {
                     connection.close();
                  }
    
                  if (initialContext != null)
                  {
                     initialContext.close();
                  }
               }