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"; } }
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(); | } } }
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(); | } } }
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!
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 © 2000-2003, Novell, Inc. All rights reserved. |