JMS Clustered Grouping Example

This example demonstrates how to ensure strict ordering across a cluster using clustered message grouping

We create 3 nodes each with a grouping message handler, one with a Local handler and 2 with a Remote handler.

The local handler acts as an arbitrator for the 2 remote handlers, holding the information on routes and communicating the routing info with the remote handlers on the other 2 nodes

We then send some messages to each node with the same group id set and ensure the same consumer receives all of them

Here's the relevant snippet from the server configuration that has the local handler

     
       <cluster-connections>
          <cluster-connection name="my-cluster">
             <address>jms</address>
             <connector-ref>netty-connector</connector-ref>
             <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>
       </cluster-connections>

       <grouping-handler name="my-grouping-handler">
          <type>LOCAL</type>
          <address>jms</address>
          <timeout>5000</timeout>
       </grouping-handler>
     
     

Here's the relevant snippet from the server configuration that has the remote handlers

     
       <cluster-connections>
          <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>
       </cluster-connections>

       <grouping-handler name="my-grouping-handler">
          <type>REMOTE</type>
          <address>jms</address>
          <timeout>5000</timeout>
       </grouping-handler>
     
     

For more information on ActiveMQ clustering and grouping see the clustering and grouping section of the user manual.

Example step-by-step

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

  1. Get an initial context for looking up JNDI from server 0.
  2.            ic0 = getContext(0);
            
  3. Look-up the JMS Queue object from JNDI
  4.            Queue queue = (Queue)ic0.lookup("/queue/exampleQueue");
            
  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. Get an initial context for looking up JNDI from server 2.
  12.            ic2 = getContext(2);
            
  13. Look-up a JMS Connection Factory object from JNDI on server 2
  14.            ConnectionFactory cf2 = (ConnectionFactory)ic2.lookup("/ConnectionFactory");
               
            
  15. We create a JMS Connection connection0 which is a connection to server 0
  16.            connection0 = cf0.createConnection();
            
  17. We create a JMS Connection connection0 which is a connection to server 1
  18.            connection1 = cf1.createConnection();
            
  19. We create a JMS Connection connection0 which is a connection to server 2
  20.            connection2 = cf2.createConnection();
            
  21. We create a JMS Session on server 0
  22.            Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  23. We create a JMS Session on server 1
  24.            Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  25. We create a JMS Session on server 2
  26.            Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  27. We start the connections to ensure delivery occurs on them
  28.            
             connection0.start();
    
             connection1.start();
    
             connection2.start();
            
  29. We create JMS MessageConsumer objects on server 0
  30.            MessageConsumer consumer = session0.createConsumer(queue);
            
  31. We create a JMS MessageProducer object on server 0, 1 and 2
  32.            
             MessageProducer producer0 = session0.createProducer(queue);
    
             MessageProducer producer1 = session1.createProducer(queue);
    
             MessageProducer producer2 = session2.createProducer(queue);
            
  33. We send some messages to server 0, 1 and 2 with the same groupid set
  34.            
             final int numMessages = 10;
    
             for (int i = 0; i < numMessages; i++)
             {
                TextMessage message = session0.createTextMessage("This is text message " + i);
    
                message.setStringProperty(ActiveMQMessage.JMSXGROUPID, "Group-0");
    
                producer0.send(message);
    
                System.out.println("Sent messages: " + message.getText() + " to node 0");
             }
    
             for (int i = 0; i < numMessages; i++)
             {
                TextMessage message = session1.createTextMessage("This is text message " + (i + 10));
    
                message.setStringProperty(ActiveMQMessage.JMSXGROUPID, "Group-0");
    
                producer1.send(message);
    
                System.out.println("Sent messages: " + message.getText() + " to node 1");
    
             }
    
             for (int i = 0; i < numMessages; i++)
             {
                TextMessage message = session2.createTextMessage("This is text message " + (i + 20));
    
                message.setStringProperty(ActiveMQMessage.JMSXGROUPID, "Group-0");
    
                producer2.send(message);
    
                System.out.println("Sent messages: " + message.getText() + " to node 2");
             }
            
            
  35. We now consume those messages from server 0. We note the messages have all been sent to the same consumer on the same node
  36.            
             for (int i = 0; i < numMessages * 3; i++)
             {
                TextMessage message0 = (TextMessage)consumer.receive(5000);
    
                System.out.println("Got message: " + message0.getText() + " from node 0");
    
             }
            
            
  37. Finally, Be sure to close our resources!
  38.            
             if (connection0 != null)
             {
                connection0.close();
             }
    
             if (connection1 != null)
             {
                connection1.close();
             }
    
             if (connection2 != null)
             {
                connection2.close();
             }
    
             if (ic0 != null)
             {
                ic0.close();
             }
    
             if (ic1 != null)
             {
                ic1.close();
             }
    
             if (ic2 != null)
             {
                ic2.close();
             }