JMS Durable Subscription Example

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 Artemis 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

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.

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 does not 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

This example uses JNDI to lookup the JMS Queue and ConnectionFactory objects. If you prefer not to use JNDI, these could be instantiated directly.

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.

     <cluster-connection name="my-cluster">
        <address>jms</address>
        <retry-interval>500</retry-interval>
        <use-duplicate-detection>true</use-duplicate-detection>
        <forward-when-no-consumers>true</forward-when-no-consumers>
        <max-hops>1</max-hops>
        <discovery-group-ref discovery-group-name="my-discovery-group"/>
     </cluster-connection>
     
     

For more information on ActiveMQ Artemis load balancing, 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 -Pexample from this directory

  1. Get an initial context for looking up JNDI from server 0.
  2.            
       ic0 = getContext(0);
       
            
  3. Look-up the JMS Topic object from JNDI
  4.            Topic topic = (Topic)ic0.lookup("/topic/exampleTopic");
            
  5. Look-up a JMS Connection Factory object from JNDI on server 0
  6.            ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");
            
  7. Get an initial context for looking up JNDI from server 1.
  8.            ic1 = getContext(1);
            
  9. Look-up a JMS Connection Factory object from JNDI on server 1
  10.            ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");
               
            
  11. We create a JMS Connection connection0 which is a connection to server 0 and set the same client-id.
  12.           
       connection0 = cf0.createConnection();
       final String clientID = "my-client-id";         
       connection0.setClientID(clientID);
              
            
  13. We create a JMS Connection connection1 which is a connection to server 1 and set the same client-id.
  14.           
       connection1 = cf1.createConnection();
       connection1.setClientID(clientID);
              
            
  15. We create a JMS Session on server 0
  16.            
       Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
               
            
  17. We create a JMS Session on server 1
  18.            
       Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
                
            
  19. We start the connections to ensure delivery occurs on them
  20.            
       connection0.start();
    
       connection1.start();
               
            
  21. We create JMS durable subscriptions with the same name and client-id on both nodes of the cluster
  22.            
       final String subscriptionName = "my-subscription";
             
       MessageConsumer subscriber0 = session0.createDurableSubscriber(topic, subscriptionName);
    
       MessageConsumer subscriber1 = session1.createDurableSubscriber(topic, subscriptionName);
               
            
  23. We create a JMS MessageProducer object on server 0.
  24.            
       MessageProducer producer = session0.createProducer(topic);
            
  25. We send some messages to server 0.
  26.            
    	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());
    	}
               
            
  27. 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.
  28.            
    	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");
    	}
               
            
  29. And finally (no pun intended), always remember to close your JMS resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  30.            
    	finally
    	{
    	   if (connection0 != null)
    	   {
    	      connection0.close();
    	   }
    	      
    	   if (connection1 != null)
    	   {
    	      connection1.close();
    	   }
    	}