サーバー・コード
サーバー・アプリケーションでは、リモート・オブジェクトのインスタンスを作成し、ネーミング・サービスに公開する必要があります。 Java™ Naming and Directory Interface (JNDI) は、標準インターフェースのセットを定義します。 このインターフェースは、ネーミング・サービスの照会や、ネーミング・サービスへのオブジェクトのバインドに使用されます。
Context ctx = new InitialContext(...); // get hold of the initial context
ctx.bind("sample", sampleReference); // bind the reference to the name "sample"
Object obj = ctx.lookup("sample"); // obtain the reference- java.naming.factory.initial
- javax.naming.Context.INITIAL_CONTEXT_FACTORY とも定義されます。このプロパティーは、ネーミング・サービス・プロバイダーの初期コンテキスト・ファクトリーのクラス名を指定します。 RMI レジストリーの場合、クラス名は com.sun.jndi.rmi.registry.RegistryContextFactory です。 CosNaming サービスの場合、クラス名は com.sun.jndi.cosnaming.CNCtxFactoryです。
- java.naming.provider.url
- このプロパティーはルート・ネーミング・コンテキスト、オブジェクト・リクエスト・ブローカー (ORB)、またはその両方を構成します。 ネーミング・サービスが異なるホストに保管されている場合に使用され、以下のような異なる URI スキームをとることができます。
- rmi
- corbaname
- corbaloc
- IOR
- iiop
- iiopname
rmi://[<host>[:<port>]][/<initial_context>] for RMI registry iiop://[<host>[:<port>]][/<cosnaming_name>] for COSNaming
Hashtable env = new Hashtable();
Env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.cosnaming.CNCtxFactory");そして、ハッシュ・テーブルを InitialContext のコンストラクターに引数として渡します。例えば、RMI (JRMP) を使用する場合は、サーバントのインスタンスを作成してから、前述の手順に従ってこの参照をネーミング・サービスにバインドします。
public class Server {
public static void main (String args []) {
try {
ORB orb = ORB.init(args, null);
// Get reference to the root poa & activate the POAManager
POA poa = (POA)orb.resolve_initial_references("RootPOA");
poa.the_POAManager().activate();
// Create a servant and register with the ORB
SampleImpl sample = new SampleImpl();
sample.setORB(orb);
// TIE model ONLY
// create a tie, with servant being the delegate and
// obtain the reference ref for the tie
SamplePOATie tie = new SamplePOATie(sample, poa);
Sample ref = tie._this(orb);
// Inheritance model ONLY
// get object reference from the servant
org.omg.CORBA.Object ref = poa.servant_to_reference(sample);
Sample ref = SampleHelper.narrow(ref);
// bind the object reference ref to the naming service using JNDI
..........(see previous code) .....
orb.run();
}
catch(Exception e) {}
}
}public class Server {
public static void main (String args []) {
try {
ORB orb = ORB.init(args, null);
// Get reference to the root poa & activate the POAManager
POA poa = (POA)orb.resolve_initial_references("RootPOA");
poa.the_POAManager().activate();
// Create servant and its tie
SampleImpl sample = new SampleImpl();
_SampleImpl_Tie tie = (_SampleImpl_Tie)Util.getTie(sample);
// get an usable object reference
org.omg.CORBA.Object ref = poa.servant_to_reference((Servant)tie);
// bind the object reference ref to the naming service using JNDI
..........(see previous code) .....
}
catch(Exception e) {}
}
}以前のポータブル・オブジェクト・アダプター (POA) サーバー・コードを使用するには、 -iiop -poa オプションを一緒に使用して、 rmic がタイを生成できるようにする必要があります。 POAを使わない場合、RMI(IIOP)サーバーコードを削減して、サーバント(SampleImpl sample = new SampleImpl())をインスタンス化できます。次に、RMI(JRMP)環境で通常行われるように、サーバントをネーミングサービスにバインドします。 この場合、-iiop オプションのみを使用して、rmic により RMI-IIOP タイを生成できるようにする必要があります。 -iiopを省略すると、RMI (JRMP) スケルトンが生成されます。
RMI-IIOP オブジェクトをご使用のサーバーにエクスポートする場合、必ずしも JRMP と IIOP のいずれかを選択する必要はありません。 JRMP クライアントおよび IIOP クライアントをサポートするシングル・サーバー・オブジェクトが必要な場合は、RMI-IIOP オブジェクトを JRMP と IIOP に同時にエクスポートできます。 RMI-IIOP 用語では、このアクションは二重エクスポートと呼ばれます。
public class SampleClient {
public static void main(String [] args) {
try{
Sample sampleref
//Look-up the naming service using JNDI and get the reference
.........
// Invoke method
System.out.println(sampleRef.message());
}
catch(Exception e) {}
}
} public class SampleClient {
public static void main (String [] args) {
try {
ORB orb = ORB.init(args, null);
// Look up the naming service using JNDI
......
// Narrowing the reference to the correct class
Sample sampleRef = SampleHelper.narrow(o);
// Method Invocation
System.out.println(sampleRef.message());
}
catch(Exception e) {}
}
}public class SampleClient {
public static void main (String [] args) {
try{
ORB orb = ORB.init(args, null);
// Retrieving reference from naming service
........
// Narrowing the reference to the correct class
Sample sampleRef = (Sample)PortableRemoteObject.narrow(o, Sample.class);
// Method Invocation
System.out.println(sampleRef.message());
}
catch(Exception e) {}
}
}