Clustering Servers for Load Balancing

This example starts up two servers on ports 8989 and 9898 respectively. Each specifies the other port as its secondary. The clustering scheme is set to Round/Robin Read/Only. The client automatically round-robins the the requests to the two servers. If a server goes down, then the client detects that and continues to invoke on the other server. If the down server comes back up, the client will detect it after about 30 seconds and start using it again.

1 Server 1

Another important thing to notice in this example is that the ior is written out only if it doesn't already exist. The is due to the fact that use of TRANSIENT and USER_ID POA policies in the ORB, results in Persistent Objects (without Server Activation).
package cluster;
                                                                           
import util.Util;
                                                                           
import java.io.File;
                                                                           
import org.omg.CORBA.Any;
import org.omg.CORBA.ORB;
import org.omg.CORBA.Policy;
                                                                           
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID;
import org.omg.PortableServer.IdAssignmentPolicyValueHelper;
                                                                           
import com.sssw.jbroker.api.transport.TCPAddress;
                                                                           
import com.sssw.jbroker.api.cluster.ClusterPolicy;
import com.sssw.jbroker.api.cluster.ClusterPolicyValue;
                                                                           
public class Server
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   int scheme = (args.length > 0 && args[0].equals("custom")) ?
    |   |       ClusterPolicyValue.CUSTOM_SCHEME : ClusterPolicyValue.RR_RO_SCHEME;
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Servant hello = new HelloImpl(orb, 1);
    |   |                                                                  
    |   |   // get the root POA
    |   |   POA rootPOA = (POA) orb.resolve_initial_references("RootPOA");
    |   |                                                                  
    |   |   // create the cluster policy value
    |   |   ClusterPolicyValue cpValue = new ClusterPolicyValue(
    |   |       new TCPAddress[] { 
    |   |           new TCPAddress("localhost", 8989) },
    |   |       new TCPAddress[] {
    |   |           new TCPAddress("localhost", 9898) },
    |   |       scheme);
    |   |   Any cpAny = orb.create_any(); cpAny.insert_Value(cpValue);
    |   |                                                                  
    |   |   // create user id assignment policy value
    |   |   Any idAny = orb.create_any();
    |   |   IdAssignmentPolicyValueHelper.insert(idAny, 
    |   |       IdAssignmentPolicyValue.USER_ID);
    |   |                                                                  
    |   |   // create a cluster POA
    |   |   POA clusterPOA = (POA) rootPOA.create_POA("clusterPOA", rootPOA.
    |   |       the_POAManager(), new Policy[] {
    |   |       |   orb.create_policy(ID_ASSIGNMENT_POLICY_ID.value, idAny),
    |   |           orb.create_policy(ClusterPolicy.POLICY_TYPE, cpAny)});
    |   |                                                                  
    |   |   // activate the hello object
    |   |   clusterPOA.activate_object_with_id("hello".getBytes(), hello);
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(
    |   |       clusterPOA.servant_to_reference(hello));
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   if (!new File("ior").exists())
    |   |       Util.writeIOR(helloIOR, "ior", true); 
    |   |                                                                  
    |   |   // activate the Root POA
    |   |   rootPOA.the_POAManager().activate();
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("server1 waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

2 Server 2

package cluster;
                                                                           
import util.Util;
                                                                           
import java.io.File;
                                                                           
import org.omg.CORBA.Any;
import org.omg.CORBA.ORB;
import org.omg.CORBA.Policy;
                                                                           
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID;
import org.omg.PortableServer.IdAssignmentPolicyValueHelper;
                                                                           
import com.sssw.jbroker.api.transport.TCPAddress;
                                                                           
import com.sssw.jbroker.api.cluster.ClusterPolicy;
import com.sssw.jbroker.api.cluster.ClusterPolicyValue;
                                                                           
public class Server2
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   int scheme = (args.length > 0 && args[0].equals("custom")) ?
    |   |       ClusterPolicyValue.CUSTOM_SCHEME : ClusterPolicyValue.RR_RO_SCHEME;
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Servant hello = new HelloImpl(orb, 2);
    |   |                                                                  
    |   |   // get the root POA
    |   |   POA rootPOA = (POA) orb.resolve_initial_references("RootPOA");
    |   |                                                                  
    |   |   // create the cluster policy value
    |   |   ClusterPolicyValue cpValue = new ClusterPolicyValue(
    |   |       new TCPAddress[] { 
    |   |           new TCPAddress("localhost", 9898) },
    |   |       new TCPAddress[] {
    |   |           new TCPAddress("localhost", 8989) },
    |   |       scheme);
    |   |   Any cpAny = orb.create_any(); cpAny.insert_Value(cpValue);
    |   |                                                                  
    |   |   // create user id assignment policy value
    |   |   Any idAny = orb.create_any();
    |   |   IdAssignmentPolicyValueHelper.insert(idAny, 
    |   |       IdAssignmentPolicyValue.USER_ID);
    |   |                                                                  
    |   |   // create a cluster POA
    |   |   POA clusterPOA = (POA) rootPOA.create_POA("clusterPOA", rootPOA.
    |   |       the_POAManager(), new Policy[] {
    |   |       |   orb.create_policy(ID_ASSIGNMENT_POLICY_ID.value, idAny),
    |   |           orb.create_policy(ClusterPolicy.POLICY_TYPE, cpAny)});
    |   |                                                                  
    |   |   // activate the hello object
    |   |   clusterPOA.activate_object_with_id("hello".getBytes(), hello);
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(
    |   |       clusterPOA.servant_to_reference(hello));
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   if (!new File("ior").exists())
    |   |       Util.writeIOR(helloIOR, "ior", true); 
    |   |                                                                  
    |   |   // activate the Root POA
    |   |   rootPOA.the_POAManager().activate();
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("server2 waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

3 Hello Implementation

package cluster;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
public class HelloImpl extends helloWorld.HelloPOA
{
    private final ORB _orb;
    private final int _server;
                                                                           
    public HelloImpl(ORB orb, int server)
    {
    |   _orb = orb;
    |   _server = server;
    }
                                                                           
    public String sayHello()
    {
    |   return "Hello World from Server " + _server + "!\n";
    }
}

4 Client

package cluster;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
import helloWorld.Hello;
import helloWorld.HelloHelper;
                                                                           
public class Client
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // read the stringified object reference
    |   |   String helloIOR = Util.readIOR("ior");
    |   |                                                                  
    |   |   // narrow the stringified object
    |   |   Hello hello = HelloHelper.narrow(orb.string_to_object(helloIOR));
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   for (int i=0; i < 10; i++)
    |   |       System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
Copyright © 2000-2003, Novell, Inc. All rights reserved.