Wednesday, November 21, 2012

Java RMI

1) Compute.java

package compute;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Compute extends Remote {
   <T> T executeTask(Task<T> t) throws RemoteException;
}

2) Task.java

package compute;

public interface Task<T> {
   T execute();
}

cd c:\home\waldo\src
javac compute\Compute.java compute\Task.java
jar cvf compute.jar compute\*.class

3) ComputeEngine.java

package engine;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import compute.Compute;
import compute.Task;

public class ComputeEngine implements Compute{
   public ComputeEngine(){
      super();
   }
  
   public<T> T executeTask(Task<T> t){
      return t.execute();
   }

   public static void main(String[] args){
      if(System.getSecurityManager() == null){
         System.setSecurityManager(new SecurityManager());
      }
      try{
         String name = "Compute";
         Compute engine = new ComputeEngine();
         Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
         Registry registry = LocateRegistry.getRegistry();
         registry.rebind(name, stub);
      } catch(Exception e){
         System.err.println("ComputeEngine exception: ");
         e.printStackTrace();
      }
   }
}

cd c:\home\ann\src
javac -cp c:\home\ann\public_html\classes\compute.jar
    engine\ComputeEngine.java

Pi.java

package client;
import compute.Task;
import java.io.Serializable;
import java.math.BigDecimal;

public class Pi implements Task<BigDecimal>, Serializable{
   private final int digits;
   public Pi(int digits){
      this.digits = digits;
   }
   public BigDecimal execute(){
      return computePi(digits);
   }
   public static BigDecimal computePi(int digits){
      return result;
   }
}

ComputPi.java

package client;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.math.BigDecimal;
import compute.Compute;

public class ComputePi{
   public static void main(String args[]){
      if(System.getSecurityManager() == null){
         System.setSecurityManager(new SecurityManager());
      }
      try{
         String name = "Compute";
         Registry registry = LocateRegistry.getRegistry(args[0]);
         Compute comp = (Compute) registry.lookup(name);
         Pi task = new Pi(Integer.parseInt(args[1]);
         BigDecimal pi = comp.executeTask(task);
         System.out.println(pi);
      } catch(Exception e){
         System.err.println("ComputerPi exception:");
         e.printStackTrace();
      }
   }
}

cd c:\home\jones\src
javac -cp c:\home\jones\public_html\classes\compute.jar
   client\ComputePi.java client\Pi.java
mkdir c:\home\jones\public_html\classes\client
cp client\Pi.class
   c:\home\jones\public_html\classes\client

server.policy

grant codeBase "file:/home/ann/src/" {
   permission java.security.AllPermission;
};

client.policy

grant codeBase "file:/home/jones/src/" {
   permission java.security.AllPermission;
};

start rmiregistry (or javaw if start is not available)

java -cp c:\home\ann\src;c:\home\ann\public_html\classes\compute.jar
       -Djava.rmi.server.codebase=file:/c:/home/ann/public_html/classes/compute.jar
       -Djava.rmi.server.hostname=mycomputer.example.com
       -Djava.security.policy=server.policy
          engine.ComputeEngine

java -cp c:\home\jones\src;c:\home\jones\public_html\classes\compute.jar
       -Djava.rmi.server.codebase=file:/c:/home/jones/public_html/classes/
       -Djava.security.policy=client.policy
          client.ComputePi mycomputer.example.com 45

No comments: