Florida State UniversityCOP Advanced Unix Programming Remote Method Invocation /rmi/index.html
Florida State UniversityCOP Advanced Unix Programming RMI Abstraction Local object Remote Object Client Server Method call Return Argument
Florida State UniversityCOP Advanced Unix Programming Behavior Vs. Implementation Interface Implementation Client program Server program RMI System
Florida State UniversityCOP Advanced Unix Programming How RMI Works Client ProgramServer Program Server Stub Server Skeleton Remote Reference Layer TCP/IP
Florida State UniversityCOP Advanced Unix Programming Local Vs. Remote Objects Object reference points to local object. Object arguments in methods are passed by reference. Garbage collection when no more local references. Object reference points to a proxy (stub) object. Object arguments in methods are passed by value. Garbage collection when no more local or remote references.
Florida State UniversityCOP Advanced Unix Programming Example : Remote Calculator
Florida State UniversityCOP Advanced Unix Programming The Interface public interface Calculator extends java.rmi.Remote { public long add(long a, long b) throws java.rmi.RemoteException; public long sub(long a, long b) throws java.rmi.RemoteException; public long mul(long a, long b) throws java.rmi.RemoteException; public long div(long a, long b) throws java.rmi.RemoteException; }
Florida State UniversityCOP Advanced Unix Programming Remote Object Implementation public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator { // Must have an explicit constructor public CalculatorImpl() throws java.rmi.RemoteException { super(); } public long add(long a, long b) throws java.rmi.RemoteException { return a + b; } public long sub(long a, long b) throws java.rmi.RemoteException { return a - b; } // Similarly implement mul() and div() }
Florida State UniversityCOP Advanced Unix Programming Alternative Remote Object Implementation public class CalculatorImpl implements Calculator //Doesn’t extend UnicastRemoteObject { // constructor doesn’t call super() – instead calls exportObject() public CalculatorImpl() { UnicastRemoteObject.exportObject(this); } public long add(long a, long b) throws java.rmi.RemoteException { return a + b; } public long sub(long a, long b) throws java.rmi.RemoteException { return a - b; } // Similarly implement mul() and div() }
Florida State UniversityCOP Advanced Unix Programming Creating Stubs and Skeletons Compile the interface % javac Calculator.java Compile the implementation % javac CalculatorImpl.java Create Stubs and Skeletons % rmic CalculatorImpl This creates CalculatorImpl_Stub.class and CalculatorImpl_Skel.class
Florida State UniversityCOP Advanced Unix Programming Server Code import java.rmi.Naming; public class CalculatorServer { public static void main(String args[]) { try { Calculator c = new CalculatorImpl(); Naming.rebind( "rmi://linprog1/CalculatorService", c); } catch (Exception e) { System.out.println("Trouble: " + e); }
Florida State UniversityCOP Advanced Unix Programming Client Code import java.rmi.*; public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator)Naming.lookup( "rmi://linprog1/CalculatorService"); System.out.println( c.sub(4, 3) ); System.out.println( c.add(4, 5) ); System.out.println( c.mul(3, 6) ); System.out.println( c.div(9, 3) ); } catch (Exception e) { System.out.println("Trouble: " + e); }
Florida State UniversityCOP Advanced Unix Programming Running it all Copy the following to server machine –CalculatorServer.class –CalculatorImpl_Skel.class –CalculatorImpl.class –Calculator.class Copy the following to client machine –CalculatorClient.class –CalculatorImpl_Stub.class –Calculator.class
Florida State UniversityCOP Advanced Unix Programming Running it all…. At server machine % rmiregistry & % java CalculatorServer At client machine % java CalculatorClient
Florida State UniversityCOP Advanced Unix Programming Parameter Passing & Return Values
Florida State UniversityCOP Advanced Unix Programming Single JVM Primitive data types –Pass by value Objects –Pass by reference –All objects in Java are on the heap.
Florida State UniversityCOP Advanced Unix Programming Between Remote JVMs Using RMI Primitive data types –By value Local Objects –By value again. –No common heap between remote JVMs! –But what’s the problem in passing objects by value?
Florida State UniversityCOP Advanced Unix Programming Serialization Flatten the object being passed and any objects it references. Need to “marshall” (copy) all members fields of objects being passed. Fields may be object references! So follow the object reference and perform a “deep-copy”.