POA Local Hello World

The ORB's compiler and runtime support local invocations via local stubs as well as using a loopback GIOP transport. In this version of the POA Hello World, we will run the client and the server in the same Java VM using the same ORB. We will see that the invocation does not switch threads even when we have stubs that are not enabled for local invocation.

1 Server

package poaLocal;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
                                                                           
public class Server
{
    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);
    |   |                                                                  
    |   |   // activate the Root POA
    |   |   rootPOA.the_POAManager().activate();
    |   |                                                                  
    |   |   // start the client thread
    |   |   ClientThread client = new ClientThread(orb);
    |   |   client.start();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

2 Client Thread

The client is started on a different Thread. The output should look some thing like this:
Invoking from Thread[Thread-5,5,main]
Hello World! from Thread[Thread-5,5,main]
package poaLocal;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
import helloWorld.Hello;
import helloWorld.HelloHelper;
                                                                           
public class ClientThread extends Thread
{
    ORB _orb;
                                                                           
    ClientThread(ORB orb)
    {
    |   _orb = orb;
    }
                                                                           
    public void run()
    {
    |   try {
    |   |                                                                  
    |   |   // read the stringified object reference
    |   |   String helloIOR = Util.readIOR("ior");
    |   |                                                                  
    |   |   // narrow the stringified object
    |   |   Hello hello = HelloHelper.narrow(_orb.string_to_object(helloIOR));
    |   |                                                                  
    |   |   // print out thread
    |   |   System.out.println("Invoking from " + Thread.currentThread());
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

3 Local Invocation with Marshaling

Try compiling the idl file with -poa -noloal options. It will generate stubs that are not enabled for local invoations. Run the Server again. You will see that the invocation still does not switch thread. This demonstrates the "short circuiting" that the ORB runtime does for local invocations even when the stub does not cooperate.
Copyright © 2000-2003, Novell, Inc. All rights reserved.