負荷分散のためのクラスタサーバ
この例では、ポート8989および9898の2つのサーバをそれぞれ起動します。 セカンダリとして互いのポートを指定します。 クラスタスキームは、Round/Robin Read/Onlyに設定されています。 クライアントは2つのサーバに要求を自動的にラウンドロビンします。 サーバが故障した場合、クライアントはそれを検出し、他方のサーバの呼び出しを続行します。 故障したサーバが復帰すると、クライアントはそれを約30秒後に検出し、それを再び起動します。
1 サーバ1
このサンプルで注意すべきもう1つの重要な点は、iorがまだ存在しない場合は、それが書き出されることです。 jBroker ORBでのTRANSIENTおよびUSER_ID POAポリシーの使用の結果、一時的オブジェクトが得られるためです(サーバ起動なし)。
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 サーバ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の実装
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 クライアント
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.
|