This example demonstrates how subsequent connections created from a JMS Connection Factory can be created to different nodes of the cluster. In other words it demonstrates how JBoss Messaging 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 JBoss Messaging will round-robin the connections can either be specified explicitly in the connection factory when creating it, or deploying it on the server, or the factory can be 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 jbm-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 JBoss Messaging load balancing, and clustering in general, please see the clustering section of the user manual.
To run the example, simply type ant
from this directory
initialContext = getContext(0);
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
connectionA = connectionFactory.createConnection();
connectionB = connectionFactory.createConnection();
connectionC = connectionFactory.createConnection();
Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sessionB = connectionB.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session sessionC = connectionC.createSession(false, Session.AUTO_ACKNOWLEDGE);
connectionA.start();
connectionB.start();
connectionC.start();
MessageConsumer consumerA = sessionA.createConsumer(queue);
MessageConsumer consumerB = sessionB.createConsumer(queue);
MessageConsumer consumerC = sessionC.createConsumer(queue);
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());
}
for (int i = 0; i < numMessages; i ++)
{
TextMessage messageA = (TextMessage)consumerA.receive(5000);
System.out.println("Got message: " + messageA.getText() + " from node A");
TextMessage messageB = (TextMessage)consumerB.receive(5000);
System.out.println("Got message: " + messageB.getText() + " from node B");
TextMessage messageC = (TextMessage)consumerC.receive(5000);
System.out.println("Got message: " + messageC.getText() + " from node C");
}
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (connection0 != null)
{
connection0.close();
}
if (connection1 != null)
{
connection1.close();
}
}