Developing Scalable Distributed Applications 
by Mario A. Torres

Listing One
public interface IntfMgr
{
    public String getHostOS() throws Exception;
    public String getHostTime() throws Exception;
}


Listing Two
import java.util.Date;
public class LocalMgr implements IntfMgr
{
    protected String strMgrType;
    LocalMgr() { strMgrType = "Local"; }
    
    public String getHostOS() throws Exception {
    return System.getProperty("os.name");
    }
    public String getHostTime () throws Exception {
    Date d = new Date();
    Return d.toString();
    }
}


Listing Three
// Client for Distributed Computing Example
import java.util.*;

public class  DCEx
{
    DCEx() { }
  public static void main (String argv[ ])
     throws Exception
  {  // Let the interface be defined by local manager
    IntfMgr mgr = new LocalMgr();
  // Execute a manager method
    String str = mgr.getHostOS();
    System.out.println("Returned Host name: "+str);
  }
}


Listing Four
// RMI Manager
import java.rmi.*;
import java.rmi.registry.*;

public class RmiMgr implements IntfMgr
{
private RmiIntf mgrRmi;
// Constructor
public RmiMgr(String host, int port) 
    throws Exception {
     String strURL = "rmi://"+host+":"+ port+"/RmiImpl";
     mgrRmi = (RmiIntf)Naming.lookup(strURL);
  }
 public String getHostOS() 
   throws Exception {
     return mgrRmi.getHostOS();
 }
 public String getHostTime() 
    throws Exception {
     return mgrRmi.getHostTime();
  }
}


Listing Five 
// Interface RmiIntf 
import java.rmi.*;

public interface RmiIntf extends Remote 
{ 
  public String getHostOS()
 throws Exception,  RemoteException;
  public String getHostTime() 
throws Exception, RemoteException;
}


Listing Six
// Implementation of the interface RmiIntf 
import java.rmi.*;
import java.util.*;

public class  RmiImpl
   extends java.rmi.server.UnicastRemoteObject
   implements RmiIntf
{
  IntfMgr mgr;

  public RmiImpl() 
    throws Exception, RemoteException {
      mgr = new LocalMgr();
  }
  public String getHostOS()
    throws Exception, RemoteException { 
      return mgr.getHostOS(); 
  }
  
  public String getHostTime()
    throws Exception, RemoteException { 

    return mgr.getHostTime();
  }
}


Listing Seven
// Client for Distributed Computing Example
import java.util.*;

public class  DCEx
{
    DCEx() { }
  public static void main ()
  { // Interface is not defined by LocalMgr anymore 
    // IntfMgr mgr = new LocalMgr();
  // manager is now defined by  RMI manager
  IntfMgr mgr = new RmiMgr("remoteHostName",  1099 );
  // Execute a manager method
    String str = mgr.getHostOS();
    System.out.println("Returned Host name: "+str);
  }
}

Listing Eight
import java.rmi.*;
import java.rmi.registry.*;

public class MyRmiServerReg
{
  public static void main(String args[])
  {
    try {
      int port=1099;
      Registry reg =
                    LocateRegistry.createRegistry(port);
      RmiImpl rmiImpl = new RmiImpl();
      Naming.rebind("//:"+port+ "/RmiImpl",  rmiImpl);
      System.out.println(
          "*** MyRmiServerReg: rmiregistry is ready "+
          " to execute remote method invocations of " +
          " RmiImpl on port# "+port);
    }
    catch (Exception e) {
      System.out.println("Rmi Server Error:" +  e.getMessage());
    }
  }
}





3

