50% developed

RMI-IIOP

From Wikibooks, open books for an open world
Jump to navigation Jump to search

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 | edit source]

Computer code Code listing 7.1: HowdyInterface.java
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 | edit source]

Computer code Code listing 7.2: HowdyImpl.java
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 | edit source]

Computer code Code listing 7.3: HowdyServer.java
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject ;
import com.sun.corba.se.internal.POA.POAORB;
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
      p.put("org.omg.CORBA.ORBClass",
          "com.sun.corba.se.internal.POA.POAORB");
      p.put("org.omg.CORBA.ORBSingletonClass",
          "com.sun.corba.se.internal.corba.ORBSingleton");

      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(
        ServantRetentionPolicyValue.RETAIN);
      POA tPOA = rootPOA.create_POA("MyTransientPOA", null, tpolicy);
          
      tPOA.the_POAManager().activate();

      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();
      initialNamingContext.rebind("HowdyService",
        tPOA.create_reference_with_id(id,
          tie._all_interfaces(tPOA,id)[0]) );
      System.out.println("Howdy Server: Ready...");
      
      orb.run();
    }
      catch (Exception e) {
        System.out.println("Error running HowdyServer: " + e);
        e.printStackTrace();
      }
  }

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

The Client Class[edit | edit source]

Computer code Code listing 7.4: HelloClient.java
import java.rmi.RemoteException;
import java.net.MalformedURLException;
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);
        e.printStackTrace();
        return;
    }
        
    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");
        e.printStackTrace();
        return;
    }

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

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


Clipboard

To do:
Add some exercises like the ones in Variables