初期参照サービスの使用

この例では、RMI-IIOPを使用したHello Worldのクライアントおよびサーバをやり直しますが、今回はディスクからの文字列化されたオブジェクト参照を使用する代わりに、COSネームサービスを使用してhelloオブジェクト参照をパブリッシュしたり取得したりします。


1 Helloサーバ

ここでサーバは最初に、パラメータとして「NameService」を持つORB.resolve初期参照を使用して、ルートNamingContextを取得します。 その後、ネームを作成し、オブジェクト参照をhelloオブジェクトにパブリッシュします。 Hello Worldレッスンでは、CORBAオブジェクトを期待するAPIで使用するには、あらかじめhelloサーバントのスタブを取得する必要があることを学習しました。
package initialRefs;
                                                                           
import util.Util;
                                                                           
import helloWorld2.Hello;
import helloWorld2.HelloImpl;
                                                                           
import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
                                                                           
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
                                                                           
import javax.rmi.PortableRemoteObject;
                                                                           
import com.sssw.jbroker.api.bootstrap.InitialReferences;
import com.sssw.jbroker.api.bootstrap.InitialReferencesService;
                                                                           
public class Server
{
    public static void main(String[] args)
    {
    |   InitialReferences         initialRefs;
    |   InitialReferencesService  insService;
    |                                                                      
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Hello hello = new HelloImpl();
    |   |                                                                  
    |   |   // get the initial references object
    |   |   insService = (InitialReferencesService) orb.
    |   |       resolve_initial_references("InitialReferencesService");
    |   |   initialRefs = insService.getInitialReferences("localhost", 2506);
    |   |                                                                  
    |   |   // get the root of the NameService
    |   |   NamingContext root = NamingContextHelper.narrow(
    |   |       initialRefs.get("NameService"));
    |   |                                                                  
    |   |   // publish the hello object reference
    |   |   NameComponent nc = new NameComponent("hello", "");
    |   |   NameComponent[] name = new NameComponent[] { nc };
    |   |   root.rebind(name, (Object) PortableRemoteObject.toStub(hello));
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

2 Helloクライアント

このクライアントはルートNamingContextも取得し、名前を解決してオブジェクト参照を取り戻します。
package initialRefs;
                                                                           
import util.Util;
                                                                           
import helloWorld2.Hello;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
                                                                           
import javax.rmi.PortableRemoteObject;
                                                                           
import com.sssw.jbroker.api.bootstrap.InitialReferences;
import com.sssw.jbroker.api.bootstrap.InitialReferencesService;
                                                                           
public class Client
{
    public static void main(String[] args)
    {
    |   InitialReferences         initialRefs;
    |   InitialReferencesService  insService;
    |                                                                      
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // get the initial references object
    |   |   insService = (InitialReferencesService) orb.
    |   |       resolve_initial_references("InitialReferencesService");
    |   |   initialRefs = insService.getInitialReferences("localhost", 2506);
    |   |                                                                  
    |   |   // get the root of the NameSpace
    |   |   NamingContext root = NamingContextHelper.narrow(initialRefs.
    |   |       get("NameService"));
    |   |                                                                  
    |   |   // get the hello object from the NameService
    |   |   NameComponent nc = new NameComponent("hello", "");
    |   |   NameComponent[] name = new NameComponent[] { nc };
    |   |   Hello hello = (Hello) root.resolve(name);
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

Copyright © 2000-2003, Novell, Inc. All rights reserved.