1 / 43

JAVA RMI (Remote Method Invocation)

JAVA RMI (Remote Method Invocation). SNU IDB Lab. Dong-Hyuk Im 2007.05.07. Contents. Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference. Related Technologies. RPC (“Remote Procedure Calls”) Developed by Sun

julian-rowe
Télécharger la présentation

JAVA RMI (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. JAVA RMI (Remote Method Invocation) SNU IDB Lab. Dong-Hyuk Im 2007.05.07

  2. Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference

  3. Related Technologies • RPC(“Remote Procedure Calls”) • Developed by Sun • Platform-specific • CORBA(“Common Object Request Broker Architecture”) • Developed by OMG • Access to non-Java objects (as well as Java) • DCOM(“Distributed Common Object Model”) • Developed by Microsoft • Access to Win32 objects

  4. Server Client remote skeleton object A proxy for B Request object B & dispatcher for B’s class Reply Remote Communication Remote Communication reference module module reference module module What is RMI? • Supports for distributed object in java language • Allows object to invoke methods on remote objects using local invocation • Implementation of remote interface

  5. Distributed Computing with RMI • Is relatively easy to use • Supports communication between different VMs, potentially across the network • Allows to develop distributed Java programs with the same syntax and semantics used for non-distributed programs

  6. Principle of RMI • RMI separates: • Definition of behaviour • Implementation of that behaviour • Each of them is allowed to run on different JVMs • Interfaces: define definition • Classes: define implementation

  7. Principle of RMI • 2 classes that implement the same interface • Service implementation on server • Service proxy on client • Client program makes method calls to proxy • RMI sends request to remote JVM • Return values are sent back to proxy / client program

  8. Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference

  9. RMI Architecture • The RMI architecture defines • how objects behave • how and when exceptions can occur • how memory is managed • how parameters are passed to and returned from remote methods

  10. RMI Architecture • 3 abstract layers: • Advantages of layer architecture: • Implementation of layers independent from each other • Each layer can be enhanced / replaced without affecting rest of the system

  11. Stub Object Server Object Stub & Skeleton Layer Marshalled parameters parameters Skeleton Object Marshalled return value Method invocation on client Method invocation on server Return value

  12. Stub • Represents the remote service implementation in the client (is a proxy) • Marshalls parameters : • Encoding parameters • Primitive Type (integer, Byte, … ) : copy by value • Reference Type (String, Object, …) : object copy • Information block from stub to skeleton • Remote object’s identifier • Parameters / the ID of method • Unmarshalls return value or exception

  13. Skeleton • Helper class on server • Generated for RMI to use • Communicates with stub across the link • Reads parameters for the method call from the link • Makes the call to the service object • Accepts the return value, writes it back to the stub

  14. Remote Reference Layer • Exists in both the RMI client and server • Provides a constant interface to the stubs and skeletons • Manages communication between stubs/skeleton • Manages references to remote objects • Threading, garbage collection … • Manages reconnection strategies if an object should become unavailable

  15. Transport Layer • Stream-based network connections that use TCP/IP • Deals with communications • For interoperability, RMI may use the OMG Internet Inter-ORB Protocol (IIOP)

  16. Remote Reference Layer Remote Reference Layer Transport Layer Transport Layer RMI Layers Java Virtual Machine Java Virtual Machine Client Object Remote Object Stub Skeleton TCP

  17. Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference

  18. Naming Remote Objects • How does a client find an RMI remote service? • Clients find remote services by using a naming or directory service, running on a well known host and port number • RMI • can use different directory services, e.g. the Java Naming and Directory Service (JNDI) • includes simple service called RMI Registry (rmiregistry, default on port 1099)

  19. RMI Flow (1/4) Client Virtual Machine Server Virtual Machine Client Remote Object Skeleton Stub Server “Fred” Registry Virtual Machine

  20. RMI Flow (2/4) 1. Server Creates Remote Object2. Server Registers Remote Object Client Virtual Machine Server Virtual Machine Client Remote Object 1 Skeleton Stub Server 2 “Fred” Registry Virtual Machine

  21. RMI Flow (3/4) Client Virtual Machine Server Virtual Machine Client Remote Object 3. Client requests object from Registry 4. Registry returns remote reference (and stub gets created) Skeleton Stub Server 3 4 “Fred” Registry Virtual Machine

  22. RMI Flow (4/4) Client Virtual Machine Server Virtual Machine Client Remote Object 5 7 Skeleton 6 Stub Server 5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object method “Fred” Registry Virtual Machine

  23. Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference

  24. Creating Remote Object (1/2) • Define a Remote Interface • extends java.rmi.Remote interface Adder extends Remote { public int add(int x, int y) throws RemoteException }

  25. Creating Remote Object (2/2) • Define a class that implements the Remote Interface • extends java.rmi.RemoteObject • or java.rmi.UnicastRemoteObject class AdderImpl extends UnicastRemoteObject implements Adder { public AdderImpl() throws RemoteException { } public int add(int x, int y) throws RemoteException { return x + y; } }

  26. Inheritance Diagram RemoteObject Object RemoteServer Remote Unicast RemoteObject Adder Implementation Extension AdderImpl

  27. Compiling Remote Classes • Compile the Java class • javac • reads .java file • produces .class file • Compile the Stub and Skeleton • rmic • reads .class file • produces _Skel.class and _Stub.class

  28. Compiling Remote Classes (Diagram) javac Adder.java (interface) Adder.class (interface classfile) AdderImpl_Skel.class (skeleton classfile) javac AdderImpl.java (remote class) AdderImpl.class (classfile) rmic AdderImpl_Stub.class (stub classfile)

  29. Registering Remote Classes • Start the registry • running process • Unix: rmiregistry & • Windows: start /m rmiregistry

  30. Registering Remote Classes • Remote object code in server • Remote reference code in client // Server AdderImpl a1 = new AdderImpl(“Add”); Naming.bind(“Add”, a1); // Client String url = “rmi://hostName/”; Adder a = (Adder) Naming.lookup(url + “Add”);

  31. RMI Security • RMI Security • Server is untrusted • Stubs could be malicious • RMISecurityManager • Protect from malicious stubs • A downloaded class is allowed to make a connection if the connection was initiated via the RMI transport // Client System.setSecurityManager( new RMISecurityManager());

  32. RMI Client Example // Client System.setSecurityManager( new RMISecurityManager()); String url = “rmi://hostName/”; Adder a = (Adder) Naming.lookup(url + “Add”); int sum = a.add(2,2); System.out.println("2+2=" + sum);

  33. RMI Program Adder.java • RMI “Adder” implement AdderImpl.java javac AdderImple.java AdderImpl.class rmic AdderImpl AdderClient.java AdderServer.java generates javac AdderClient.java Adder.java javac Adder.java AdderServer.java Adder.class Adder.class AdderImpl_Stub.class AdderImpl_Skel.class AdderServer.class AdderClient.class AdderImpl.class Client Server

  34. Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference

  35. Parameters in RMI • Primitive types • Passed by value • Remote objects • Passed by reference : references to remote object can be passed and returned from method calls • Non-remote objects • Passed by value • Uses Java Object Serialization : java.io.Serializable

  36. Distributed Garbage Collection • Reference counting • The server keeps track of which clients have requested • When a reference is made, the server marks the object as “dirty” • When a client drops the reference, it is marked as “clean” • Each reference has a lease with a specified time • When the lease term expires : defaults to 10 minutes • The reference is considered to be dead • The remote object may be garbage collected

  37. Distributed Garbage Collection Skeleton (maintains reference counter) Process P1 Remote Object Proxy +1 Process P2 Proxy +1

  38. Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference

  39. RMI : Benefits • Enables use of Design Patterns • Use the full power of object oriented technology in distributed computing(pass behavior and use OO design patterns) • Safe and Secure • RMI uses built-in Java security mechanisms • Easy to Write/Easy to Use • A remote interface is an actual Java interface • Distributed Garbage Collection • Collects remote server objects that are no longer referenced by any client in the network

  40. Limitation of RMI • Tied only to platforms with Java support • Can only operate with Java systems - no support for legacy systems written in C++, Fortran, Cobol, and others future languages • Uses TCP, not UDP

  41. RMI vs CORBA • Comparing RMI and CORBA doesn't reveal an optimum solution - one is not "better" than the other • CORBA • Language-neutral • Interoperability • RMI • Specifically for Java • Best for Java distributed object computing

  42. Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference

  43. References • “CORE JAVA”, Gary Cornell, Cay S. Horstmann • “JAVA RMI”, William Grosso • RMI tutorial • http://java.sun.com/docs/books/tutorial/rmi/index.html • Java Remote Invocation (RMI) • http://java.sun.com/javase/6/docs/technotes/guides/rmi/index.html

More Related