1 DISTRIBUTED SYSTEMS Practical Lab
Lab 03 - Schedule 2 Discussion of Homework 03 – Part A Discussion of open issues regarding HW 03 – Part B Introduction to RMI General overview Small example Overview on Homework 04
Remote Method Invocation 3 A pragmatic Introduction
RMI - Overview 4 Simple idea: In an OO-program objects communicate via methods For remote communication – why not allow an object to invoke a method of an object within another JVM => Remote Method Invocation (RMI) JVM - A JVM - B Java Objects Java Objects Middelware RMI RMI UDP / TCP / ?
Detailed Structure & Terminology 5 Client JVM Server JVM W X A B Y Objects Z marshalling/ unmarshalling Stub Skeleton B‘ B‘‘ RMI Transport Layer UDP/TCP/IP
Creating an RMI application 6 Step 1) Define Remote Interface Has to extend java.rmi.Remote (marker interface) Every method has to throw a RemoteException Only those methods are available remotely public interface Service extends Remote { public int increment(int value) throws RemoteException; } Parameter can be: Serializeable objects => will be serialized and transferred Remote objects => remote reference / stub will be transferred
Creating an RMI Server 7 Step 2) Implement the Remote Interface Any class may implement the interface – no restrictions Step 3) Export the remote object Initializes the RMI Environment / Transport Layer Application will not terminate while an object is exported e.g. using UnicastRemoteObject.exportObject (…) Step 4) publish the remote object within a registry Optional step, depending on the use case => see source code example
The RMI Registry 8 Problem: how to obtain references for remote objects? A) from remote services via RMI (most frequent case) B) very first reference: RMI registry RMI Registry - simple, centralized naming service Essentially a map between names (strings) and stubs Can be accessed using RMI (lookup, bind, …) A remote reference to access a registry can be constructed using a hostname and a port number Can be an independent process (rmiregistry) or embedded within a Java application (see example) The RMI Registry is handled using the LocateRegistry utility class
Creating an RMI Client 9 Step 1) Obtain a remote object reference e.g. by using an RMI registry, LDAP , … Step 2) Use the remote object within your application Invoke methods on the remote object just like for local objects (stub implements the remote interface) Remote object references may be maintained within data structures (sets, lists, maps, …) References can be forwarded to other services via arguments References can also be stored within a file (they are serializeable)
RMI Applications 10 Not limited to a Client/Server architecture Every peer might provide and use remote services Remote references can be exchanged between peers e.g. references may be used within routing tables RMI only supports synchronous method invocations Simply because Java is only supporting those Usual means to circumvent this limitation may be applied
Advanced Topics 11 Distributed Garbage Collection Unused remote objects are detected and removed Based on Leases with a long lease time Dynamic Class Loading Load code from a remote location on demand (e.g. for arguments) Remote Object Activation Create objects on client demand (no need to run them all the time) Transport Layer and interaction with alternative middleware Encryption using SSL, firwalls , proxies, … Interaction with CORBA
Further References 12 For a more extensive description see The Java RMI tutorial: http://download.oracle.com/javase/tutorial/rmi/ Java ist auch eine Insel – Chapter 19 (German) http://openbook.galileocomputing.de/javainsel8/javainsel_ 19_001.htm RMI Specification http://download.oracle.com/javase/6/docs/platform/rmi/s pec/rmiTOC.html
Recommend
More recommend