JNDIを使用したHello World

この例では、RMI-IIOPを使用したHello Worldのクライアントおよびサーバを再実行しますが、今回はCOS Namingを直接使用する代わりに、JNDIを使用してhelloオブジェクト参照を発行または取得します。

Makefileでは、JavaSoftからJNDI/COSプロバイダに初期コンテキストのJNDI Namingファクトリを設定しています。

-Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory

1 Helloサーバ

package jndiCOS;
                                                                           
import util.Util;
import helloWorld2.Hello;
import helloWorld2.HelloImpl;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
                                                                           
public class helloServer
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Hello hello = new HelloImpl();
    |   |                                                                  
    |   |   // create the JNDI Environment
    |   |   Hashtable env = new Hashtable();
    |   |   env.put("java.naming.corba.orb", orb);
    |   |                                                                  
    |   |   // get the root naming context
    |   |   Context ctx = new InitialContext(env);
    |   |                                                                  
    |   |   // bind the hello objref using JNDI
    |   |   ctx.rebind("hello", hello);
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

2 Helloクライアント

package jndiCOS;
                                                                           
import util.Util;
import helloWorld2.Hello;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
import java.util.Hashtable;
                                                                           
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
                                                                           
public class helloClient
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create the JNDI Environment
    |   |   Hashtable env = new Hashtable(5, 0.75f);
    |   |   env.put("java.naming.corba.orb", orb);
    |   |                                                                  
    |   |   // get the root naming context
    |   |   Context ctx = new InitialContext(env);
    |   |                                                                  
    |   |   // lookup the hello objref
    |   |   Hello hello = (Hello) ctx.lookup("hello");
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
Copyright © 2000-2003, Novell, Inc.All rights reserved.