1 / 20

Remote Method Invocation

Remote Method Invocation. http://developer.java.sun.com/developer/ onlineTraining/rmi/RMI.html. RMI Abstraction. Server. Client. Remote Object. Local object. Method call. Return Argument. Behavior Vs. Implementation. Server program. Client program. Implementation. Interface.

Download Presentation

Remote Method Invocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Remote Method Invocation http://developer.java.sun.com/developer/ onlineTraining/rmi/RMI.html

  2. RMI Abstraction Server Client Remote Object Local object Method call Return Argument

  3. Behavior Vs. Implementation Server program Client program Implementation Interface RMI System

  4. How RMI works?

  5. Example : Remote Calculator

  6. 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; }

  7. 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 declare mul() and div() }

  8. 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

  9. Server Code import java.rmi.Naming; public class CalculatorServer { public CalculatorServer() { try { Calculator c = new CalculatorImpl(); Naming.rebind( "rmi://sbpub1:1099/CalculatorService", c); } catch (Exception e) { System.out.println("Trouble: " + e); } } public static void main(String args[]) { new CalculatorServer(); } }

  10. Client Code import java.rmi.*; public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator)Naming.lookup( "rmi://sbpub1/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); } }

  11. Running it all • Copy the following to server machine • CalculatorServer.class • CalculatorImpl_Skel.class • CalculatorImpl_Stub.class • Calculator.class • Copy the following to client machine • CalculatorClient.class • CalculatorImpl_Stub.class • Calculator.class

  12. Running it all…. • At server machine % rmiregistry & % java CalculatorServer • At client machine % java CalculatorClient

  13. Parameter Passing &Return Values

  14. Single JVM • Primitive data types • Pass by value • Objects • Pass by reference • All objects in Java are on the heap.

  15. Between Remote JVMsUsing 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?

  16. 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.

  17. Remote Object Passing Server Naming.lookup() Client A Client B

  18. Remote Object Passing Server Naming.lookup() Client A Client B Return Value of another RMI

  19. Remote Object Passing Server Naming.lookup() Client A Client B

  20. 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. Local Vs. Remote Objects

More Related