440 likes | 830 Views
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
E N D
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 • 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
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
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
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
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
Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference
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
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
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
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
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
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
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)
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
Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference
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)
RMI Flow (1/4) Client Virtual Machine Server Virtual Machine Client Remote Object Skeleton Stub Server “Fred” Registry Virtual Machine
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
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
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
Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference
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 }
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; } }
Inheritance Diagram RemoteObject Object RemoteServer Remote Unicast RemoteObject Adder Implementation Extension AdderImpl
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
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)
Registering Remote Classes • Start the registry • running process • Unix: rmiregistry & • Windows: start /m rmiregistry
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”);
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());
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);
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
Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference
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
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
Distributed Garbage Collection Skeleton (maintains reference counter) Process P1 Remote Object Proxy +1 Process P2 Proxy +1
Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference
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
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
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
Contents • Introduction • RMI Architecture • Naming Remote Objects • Using RMI : Example • Parameters & Garbage Collection • Summary • Reference
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