Navigate Concurrent Programming topic: v  d  e )

RMI-IIOP (RMI over IIOP) is provided with the Java SDK. It combines Remote Method Invocation (RMI) technology with the Internet Inter-Orb Protocol (IIOP) to provide CORBA to the Java platform. Java developers do not have to provide IDL in order to provide CORBA capabilities.

The Remote Interface edit

Computer code Code listing 7.1:
import java.rmi.Remote;

public interface HowdyInterface extends java.rmi.Remote {
  public void sayHowdy() throws RemoteException;

The above code defines a remote interface called HowdyInterface that will define what the remote client may call on the server. All of the operations must throw a RemoteException. The interface must extend java.rmi.Remote.

The Implementation Class edit

  Code listing 7.2:
import javax.rmi.PortableRemoteObject;
import javax.rmi.RemoteException;

public class HowdyImpl implements HowdyInterface {
  public HelloImpl() throws java.rmi.RemoteException {
    PortableRemoteObject.exportObject(this);     // Initializes the remote object

  public void sayHowdy() throws RemoteException {
    System.out.println("Weeee doggies! Howdy!!");

The implementation class allows for the object to be ORB-initialized. It also implements the remote operation to be called. The implementation could have extended PortableRemoteObject, in which case the exportObject call in the constructor would be removed. The better approach would seem to be as coded above.

The Server Class edit

  Code listing 7.3:
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject ;
import org.omg.PortableServer.*;
import java.util.*;
import org.omg.CORBA.*;
import javax.rmi.CORBA.Stub;
import javax.rmi.CORBA.Util;

public class HowdyServer {
  public HowdyServer(String[] args) {
    try {
      Properties p = System.getProperties();
      // add runtime properties here

      ORB orb = ORB.init( args, p );

      POA rootPOA = (POA)orb.resolve_initial_references("RootPOA");

      Policy[] tpolicy = new Policy[3];
      tpolicy[0] = rootPOA.create_lifespan_policy(
        LifespanPolicyValue.TRANSIENT );
      tpolicy[1] = rootPOA.create_request_processing_policy(
        RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY );
      tpolicy[2] = rootPOA.create_servant_retention_policy(
      POA tPOA = rootPOA.create_POA("MyTransientPOA", null, tpolicy);

      HowdyImpl howdyImpl = new HowdyImpl();
      _HowdyImpl_Tie tie = (_HowdyImpl_Tie)Util.getTie( howdyImpl );
      String howdyId = "howdy";
      byte[] id = howdyId.getBytes();
      tPOA.activate_object_with_id( id, tie );

      Context initialNamingContext = new InitialContext();
          tie._all_interfaces(tPOA,id)[0]) );
      System.out.println("Howdy Server: Ready...");
      catch (Exception e) {
        System.out.println("Error running HowdyServer: " + e);

  public static void main(String args[]) {
    new HowdyServer( args );

The Client Class edit

  Code listing 7.4:
import java.rmi.RemoteException;
import java.rmi.NotBoundException;
import javax.rmi.*;
import java.util.Vector;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import javax.naming.Context;

public class HelloClient {

  public static void  main( String args[] ) {
    Context ic;
    Object objref;
    HelloInterface hi;

    try {
      ic = new InitialContext();
    } catch (NamingException e) {
        System.out.println("failed to obtain context" + e);
    try {
      objref = ic.lookup("HowdyService");
      System.out.println("Client: Obtained a reference to Howdy server.");
    } catch (NamingException e) {
        System.out.println("failed to lookup object reference");

    try {
      hi = (HowdyInterface) PortableRemoteObject.narrow(
        objref, HowdyInterface.class);
    } catch (ClassCastException e) {
        System.out.println("narrow failed");
      } catch( Exception e ) {
            System.err.println( "Exception " + e + "Caught" );
            e.printStackTrace( );

The client code uses the name service to look up the server and make the remote invocation.


To do:
Add some exercises like the ones in Variables