This example demonstrates how connnections created from a single JMS Connection factory can be created to different nodes of the cluster. In other words it demonstrates how ActiveMQ does client side load balancing of connections across the cluster.
The particular load-balancing policy can be chosen to be random, round-robin or user-defined. Please see the user guide for more details of how to configure the specific load-balancing policy. In this example we will use the default round-robin load balancing policy.
The list of servers over which ActiveMQ will round-robin the connections can either be specified explicitly in the connection factory when instantiating it directly, when configuring it on the server or configured to use UDP discovery to discover the list of servers over which to round-robin. This example will use UDP discovery to obtain the list.
This example starts three servers which all broadcast their location using UDP discovery. The UDP broadcast configuration
can be seen in the hornetq-configuration.xml
file.
A JMS ConnectionFactory is deployed on each server specifying the discovery group that will be used by that connection factory.
For more information on ActiveMQ load balancing, and clustering in general, please see the clustering section of the user manual.
To run the example, simply type mvn verify
from this directory
initialContext = getContext(0);
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
Connection conn = connectionFactory.createConnection();
connectionA = connectionFactory.createConnection();
connectionB = connectionFactory.createConnection();
connectionC = connectionFactory.createConnection();
conn.close();
Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sessionB = connectionB.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sessionC = connectionC.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producerA = sessionA.createProducer(queue);
MessageProducer producerB = sessionB.createProducer(queue);
MessageProducer producerC = sessionC.createProducer(queue);
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage messageA = sessionA.createTextMessage("A:This is text message " + i);
producerA.send(messageA);
System.out.println("Sent message: " + messageA.getText());
TextMessage messageB = sessionB.createTextMessage("B:This is text message " + i);
producerB.send(messageB);
System.out.println("Sent message: " + messageB.getText());
TextMessage messageC = sessionC.createTextMessage("C:This is text message " + i);
producerC.send(messageC);
System.out.println("Sent message: " + messageC.getText());
}
connectionA.start();
connectionB.start();
connectionC.start();
consume(sessionA, queue, numMessages, "A");
consume(sessionB, queue, numMessages, "B");
consume(sessionC, queue, numMessages, "C");
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (connectionA != null)
{
connectionA.close();
}
if (connectionB != null)
{
connectionB.close();
}
if (connectionC != null)
{
connectionC.close();
}
if (initialContext != null)
{
initialContext.close();
}
}