JMS Load Balanced Static Clustered One Way Queue Example

This example demonstrates a JMS queue deployed on three different nodes. The three nodes are configured to form a one way cluster from a static list of nodes.

A one way cluster is different from a symmetrical cluster in that each node is only connected to one another node in a chain type fashion, so server 0 -> server 1 -> server 2

We then create a consumer on the queue on each node, and we create a producer on only one of the nodes.

We then send some messages via the producer, and we verify that all consumers receive the sent messages in a round-robin fashion.

In other words, ActiveMQ Artemis load balances the sent messages across all consumers on the cluster

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 one way cluster between the three nodes and to load balance the messages between the nodes. Note that we have set allow-direct-connections-only to true, this means that this server will only ever connect the address's specified in the list of connectors. ALso notice that max-hops is 2, this is because server 0 is not directly connected to server 2, 2 hops in fact, so we allow any updates from servers up to 2 hops away

     
     <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>2</max-hops>
        <static-connectors allow-direct-connections-only="true">
            <connector-ref>server1-connector</connector-ref>
         </static-connectors>
     </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 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. grab an initial connection and wait, in reality you wouldn't do it this way but since we want to ensure an equal load balance we do this and then create 4 connections round robined
  8.            
       initialConnection = cf0.createConnection();
               
            
  9. We create a JMS Connection connection0 which is a connection to server 0
  10.            
       connection0 = cf0.createConnection()
               
            
  11. We create a JMS Connection connection0 which is a connection to server 1
  12.            
       connection1 = cf0.createConnection()
               
            
  13. We create a JMS Connection connection0 which is a connection to server 2
  14.            
       connection2 = cf0.createConnection()
               
            
  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 create a JMS Session on server 1
  20.            
       Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
                
            
  21. We start the connections to ensure delivery occurs on them
  22.            
       connection0.start();
    
       connection1.start();
    
       connection2.start();
               
            
  23. We create JMS MessageConsumer objects on server 0 and server 1
  24.            
       MessageConsumer consumer0 = session0.createConsumer(queue);
    
       MessageConsumer consumer2 = session2.createConsumer(queue);
    
       MessageConsumer consumer3 = session3.createConsumer(queue);
               
            
  25. We create a JMS MessageProducer object on server 0.
  26.            
       Session sendSession = getServerConnection(0, connection0, connection1, connection2).createSession(false, Session.AUTO_ACKNOWLEDGE);
    
       MessageProducer producer = sendSession.createProducer(queue);
               
            
  27. We send some messages to server 0.
  28.            
    	final int numMessages = 18;
    
    	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());
    	}
               
            
  29. We now consume those messages on *both* server 0 and server 1. We note the messages have been distributed between servers in a round robin fashion. ActiveMQ Artemis has load balanced the messages between the available consumers on the different nodes. ActiveMQ Artemis can be configured to always load balance messages to all nodes, or to only balance messages to nodes which have consumers with no or matching selectors. See the user manual for more details.
  30. JMS Queues implement point-to-point message where each message is only ever consumed by a maximum of one consumer.
               
    	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");
    
    	   TextMessage message2 = (TextMessage)consumer2.receive(5000);
    
    	   System.out.println("Got message: " + message2.getText() + " from node " + con2Node);
    	}
               
            
  31. 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
  32.            
        finally
        {
          if (initialConnection != null)
          {
             initialConnection.close();
          }
    
          if (connection0 != null)
          {
             connection0.close();
          }
    
          if (connection1 != null)
          {
             connection1.close();
          }
    
          if (connection2 != null)
          {
             connection2.close();
          }
    
          if (ic0 != null)
          {
             ic0.close();
          }
        }