In this example we will bind multiple objects to the same name in the namespace. Everytime the client resolves the name, it can potentially get a different object.
We will modify the Hello implementatin so that we know which instance of the Hello object we are talking to.
package objectGroup; import helloWorld2.Hello; import java.rmi.RemoteException; import javax.rmi.PortableRemoteObject; public class HelloImpl extends PortableRemoteObject implements Hello { private int _i; HelloImpl(int i) throws RemoteException { | _i = i; } public String sayHello() throws RemoteException { | return "Hello World from " + _i + "!\n"; } }
We will first create a in-process naming service. We will then bind two
Hello objects with the same name using add
method. We will
then publish the root of the NameService using IRS.
Note that in this example we used two objects running in the same process. We could have bound objects running in different processes and possibly even running on different machines.
package objectGroup; import javax.rmi.PortableRemoteObject; import com.sssw.jbroker.api.naming.Access; import com.sssw.jbroker.api.naming.NameService; import com.sssw.jbroker.api.naming.AccessPolicy; import com.sssw.jbroker.api.naming.NameServiceFactory; import org.omg.CORBA.ORB; import org.omg.CORBA.Object; import org.omg.CosNaming.NameComponent; import org.omg.PortableServer.POA; import org.omg.PortableServer.LifespanPolicyValue; import com.sssw.jbroker.api.naming.NamingContext; import com.sssw.jbroker.api.bootstrap.InitialReferencesService; import helloWorld2.Hello; public class helloServer { public static void main(String[] args) { | try { | | // create the ORB | | ORB orb = (ORB) org.omg.CORBA.ORB.init(args, null); | | | | // get the NameService Factory | | NameServiceFactory factory = (NameServiceFactory) orb. | | resolve_initial_references("NameServiceFactory"); | | | | // get the root POA and activate it | | POA rootPOA = (POA) orb.resolve_initial_references("RootPOA"); | | rootPOA.the_POAManager().activate(); | | | | // create a transient in-process NameService | | NameService nameService = factory.createNameService( | | rootPOA, "ns1", LifespanPolicyValue.TRANSIENT, null, null); | | | | // print the root naming context | | NamingContext rootCtx = nameService.getRootNamingContext(); | | System.out.println(orb.object_to_string(rootCtx)); | | | | // publish some objects in it | | Hello hello1 = new HelloImpl(1); | | Hello hello2 = new HelloImpl(2); | | NameComponent nc = new NameComponent("hello", ""); | | NameComponent[] name = new NameComponent[] { nc }; | | rootCtx.add(name, (Object) PortableRemoteObject.toStub(hello1)); | | rootCtx.add(name, (Object) PortableRemoteObject.toStub(hello2)); | | | | // publish root naming context in initial naming | | InitialReferencesService irs = (InitialReferencesService) orb. | | resolve_initial_references("InitialReferencesService"); | | irs.bind("NameService", rootCtx, false); | | | | // wait for invocations | | orb.run(); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
The client is using standard JNDI/COS backend to access the Hello objects.
package objectGroup; 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); | | | | for (int i=0; i < 10; i++) { | | | // 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(); | } } }
When the client is run, we get the following output which shows how the requests from the client were "round-robined" to the two different servants.
Hello World from 1! Hello World from 2! Hello World from 1! Hello World from 2! Hello World from 1! Hello World from 2! Hello World from 1! Hello World from 2! Hello World from 1! Hello World from 2!
In this section we will rewrite the client without explicitly using
any of the CORBA APIs. For this example to work, we will need to set
the system property java.naming.provider.url
to
iiop://localhost:7777
.
If you run this client, you will again see that the client requests are "round-robined" to the two different servants.
package objectGroup; 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; // // Note: set -Djava.naming.provider.url=iiop://localhost:7777 // public class helloClient2 { public static void main(String[] args) { | try { | | | | // get the root naming context | | Context ctx = new InitialContext(); | | | | for (int i=0; i < 10; i++) { | | | | | | // lookup the object | | | Hello hello = (Hello) ctx.lookup("hello"); | | | | | | // invoke method on the object | | | System.out.println(hello.sayHello()); | | } | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.