Hello World using Java RMI and POA

With the ORB you write your interfaces in Java and also benefit from the power of the Portable Object Adapter.

We will redo the Hello World application but this time we will use POA to create object references.

1 Hello RMI Interface

The Hello World interface defined using Java RMI.
package helloWorld2;
                                                                           
public interface Hello extends java.rmi.Remote
{
    String sayHello() throws java.rmi.RemoteException;
}

2 Generating Stub and Skeleton

To generate stubs and skeletons, run the rmi2iiop compiler. The rmi2iiop compiler works with compiled Java class files.
rmi2iiop -poa -keepgenerated -ds gensrc -d ../../lib helloWorld2.Hello
The following Java classes are genereted in the helloWorld2 package.
_Hello_Stub     // the Stub
HelloPOA        // the inheritance based POA Skeleton
HelloPOATie     // the delegation based POA Skeleton

3 Hello Implementation

The HelloImpl extends the Hello_Skel interface.
package poaHello2;
                                                                           
public class HelloImpl extends helloWorld2.HelloPOA
{
    public String sayHello() throws java.rmi.RemoteException
    {
    |   return "Hello World!\n";
    }
}

4 Hello World Server

The server creates the ORB, creates a POA servant, gets the root POA, creates an object reference, stringifies it, and writes it out, and then waits for invocations.

Notice, that since HelloImpl is not a CORBA object (does not implement org.omg.CORBA.Object,) you need to first get its object reference using the POA object creation APIs.

package poaHello2;
                                                                           
import util.Util;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
                                                                           
public class helloServer
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Servant hello = new HelloImpl();
    |   |                                                                  
    |   |   // get the root POA
    |   |   POA rootPOA = (POA) orb.resolve_initial_references("RootPOA");
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(rootPOA.
    |   |       servant_to_reference(hello));
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   Util.writeIOR(helloIOR, "ior", true);
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

5 Hello World Client

Since the RMI interface hasn't changed, we will use the client from Hello World using RMI-IIOP.
package helloWorld2;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
public class helloClient
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // read the stringified object reference
    |   |   String helloIOR = Util.readIOR("ior");
    |   |                                                                  
    |   |   // narrow the stringified object
    |   |   Hello hello = (Hello) orb.string_to_object(helloIOR);
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

6 TIE Approach

The TIE based approach is left as an exercise.
Copyright © 2000-2003, Novell, Inc. All rights reserved.