Broker 10.15 | webMethods Broker Documentation | webMethods Broker Messaging Programmer's Guide | JMS Policy Based Client-Side Clustering | Samples | Sample 1: Defining Weighted Round Robin Policy in a Cluster
 
Sample 1: Defining Weighted Round Robin Policy in a Cluster
This sample demonstrates how you can configure your cluster to use the weighted round robin policy using the webMethods API for JMS.
*To configure a cluster to use the weighted round robin policy
1. Create the connection factory using the webMethods API for JMS.
2. Set the load balancing policy as weighted round robin for the connection factory.
3. Define the Brokers in the cluster.
4. Set the weights for each Broker in the cluster and create the weights list in the connection factory.
5. Create the JMS connection, JMS session, and the message producer.
6. Publish messages using the message producer.
// create the connection factory
WmConnectionFactory factory = new WmConnectionFactoryImpl();
factory.setClientGroup(clientGroup);
factory.setBrokerCluster( new String[] {"Broker #1@localhost:6849",
"Broker2@localhost:6949",
"Broker3@localhost:7010"} );

// set the load balancing policy as Weighted Round Robin
factory.setClusterPolicy(WmClusterPolicyManager.WEIGHTED_ROUND_ROBIN);

// Set the weight for each Broker in the cluster
CopyOnWriteArrayList<WeightedRoundRobin> weightList = new
CopyOnWriteArrayList<WeightedRoundRobin>();

WeightedRoundRobin wrr1 = new WeightedRoundRobin();
wrr1.setBrokerName("Broker #1@localhost:6849");
wrr1.setWeight(Integer.parseInt("1"));

WeightedRoundRobin wrr2 = new WeightedRoundRobin();
wrr2.setBrokerName("Broker2@localhost:6949");
wrr2.setWeight(Integer.parseInt("2"));

WeightedRoundRobin wrr3 = new WeightedRoundRobin();
wrr3.setBrokerName("Broker3@localhost:7010");
wrr3.setWeight(Integer.parseInt("2"));

weightList.add(wrr1);
weightList.add(wrr2);
weightList.add(wrr3);

// Set the weights for the factory
factory.setWeightedRoundRobinList(weightList);

// Create the JMS Connection
connection = (Connection) factory.createConnection();
connection.start();

// Create the JMS session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);

// publish JMS Messages
for ( int i =0; i< 10000; i++) {
producer.send(session.createTextMessage());
}

// For the above weights set on the Brokers,
// BrokerA should get 2000 documents
// BrokerB should get 4000 documents
// BrokerC should get 4000 documents