260 likes | 526 Views
RMI. Java Remote Method Invocation. Java RMI. Remote method invocation addresses the incorporation of the network into a programming language, a key issue in network computing. RMI Goals. Support seamless remote method invocation on objects in different virtual machines.
E N D
RMI Java Remote Method Invocation
Java RMI • Remote method invocation addresses the incorporation of the network into a programming language, a key issue in network computing.
RMI Goals • Support seamless remote method invocation on objects in different virtual machines. • Support callbacks from servers to applets. • Integrate the distributed object model into the Java language in a natural way while retaining most of the Java language’s object semantics. • Make differences between the distributed object model and local Java object model apparent. • Make writing reliable distributed applications as simple as possible. • Preserve the safety provided by the Java runtime environment.
RMI Extended Goals • Garbage collection of remote objects • Server replication • Activation of persistent objects to service an invocation • Simple invocation to a single object or invocation to an object replicated at multiple locations.
Stubs and Skeletons Client Side Server Side Network Client m Stub Skeleton m Server
Tasks of the Stub • Initiating a call to the remote object • Marshaling arguments to a marshal stream • Informing the remote reference layer that the call should be invoked. • Unmarshaling the return value or exception from a marshal stream. • Informing the remote reference layer that the call is complete.
Tasks of the Skeleton • Unmarshaling arguments from the marshal stream. • Making the up-call to the actual remote object implementation • Marshaling the return value of the call or an exception (if one occurred) onto the marshal stream.
How to get Stub/Skeletons? • Specify the protocol of the server object • Interface definition language: Java • Interface has to extend java.rmi.Remote • Automatically generate Stub/Skeleton code • RMI Compiler
Parameter Passing in RMI • Pass by reference • Remote objects implement Remote interface • Pass by value (copy) • Basic Types • Non-remote objects are passed as copieshas toimplement java.io.Serializable
Implementing a Remote Interface • The class usually extendsjava.rmi.server.UnicastRemoteObject,thereby inheriting the remote behavior provided by the classesjava.rmi.server.RemoteObject andjava.rmi.server.RemoteServer. • The class can implement any number of remote interfaces. • The class can extend another remote implementation class. • The class can define methods that do not appear in the remote interface, but those methods can only be used locally and are not available remotely.
Locating Remote Objects • Simple name server provided • Server side:BankAccount acct = new BankAcctImpl();String url = "account";// bind url to objectjava.rmi.Naming.bind(url, acct);… • Client Side// lookup accountacct =(BankAccount) java.rmi.Naming.lookup(“rmi://localhost/account”);
RMI Registry • RMI uses a network-based registry to keep track of the distributed objects. • The server object makes a method available for remote invocation by binding itself to a name in the registry. • The client object can check for availability of an object by looking up its name in the registry. • The registry acts as a limited central management point for RMI. • The registry is simply a name repository. It does not address the problem of actually invoking the remote method.
How RMI works ... • When a client invokes a server method, the JVM looks at the stub to do type checking (since the class defined within the stub is an image of the server class). • The request is then routed to the skeleton on the server, which in turn calls the appropriate method on the server object. • The stub acts as a proxy to the skeleton and the skeleton is a proxy to the actual remote method.
Sample Application -client package ncworld.rmi1; import java.rmi.*; import java.rmi.server.*; public class Client1 { public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try {Server1 ro = (Server1) Naming.lookup("doSomething"); System.out.println("Location: ” + System.getProperty("LOCATION")); ro.doSomething(); } catch (Exception e){ e.printStackTrace(); System.exit(-1);} } }
About the Sample-client • Client: invokes the doSomething() method of a remote object (of type Server1). • All RMI-based applications will need to import java.rmi and java.rmi.server packages. • Security manager: grants or denies permissions on the operations performed by the application • If you don't set the security manager, RMI will only load classes from local system files as defined by CLASSPATH.
About the Sample-client ... • try/catch block: • binds remote object to variable ro (type: Server1) • performs the remote method invocation • The client can then use ro and invoke its methods as if it was a local object. • The client application invokes the doSomething method of the object ro, which in turn invokes the method with the same name on the Server1 object.
Server interface package ncworld.rmi1; import java.rmi.*; public interface Server1 extends Remote { public void doSomething() throws RemoteException; }
About the Sample - Server • Interface Server1 is used in the client application to declare the remote object type. • All remote objects are referenced through interfaces. • Two points must be made about the Server1 interface • It extends the Remote interface as all RMI interfaces must; and • the method doSomething() throws RemoteException which all remote operations must be able to handle.
Server Implementation (1) package ncworld.rmi1; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; public class Server1Impl extends java.rmi.server.UnicastRemoteObject implements Server1 { public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try {Server1Impl obj = new Server1Impl(); Naming.rebind("doSomething", obj); //Naming.bind also possible System.out.println("doSomething bound in registry"); } catch (Exception e) { e.printStackTrace();} }
Server Implementation (2) public Server1Impl() throws RemoteException { } public void doSomething() throws RemoteException { System.out.println("This message is printed by the Server1 object"); System.out.println("Location: " + System.getProperty("LOCATION")); }
About the Sample - Server Implementation • The Server1Impl class extends the UnicastRemoteObject class. • non-replicated remote object whose references are valid only while the server process is alive • point-to-point active object references via TCP streams. • instantiates an object of type Server1Impl • register object using the name doSomething. • A remote implementation class must have a zero-argument constructor that may throw RemoteException. • Finally, the doSomething method is defined • it throws RemoteException. • this remote method doesn't really do anything. • In a more meaningful application, the remote method may perform a query on a database, or read data from a file and process that data.
Deployment with JDK • We have a client, a server, and an implementation of the server interface. • We still need two more pieces -- the stub and the skeleton. • JDK 1.1.x up contains tool to generate these rmic rmi1.Server1Impl • This will create the two files • ServerImpl_Stub.class • ServerImpl_Skel.class. • Should you want to see the Java source code for these files, use the -keepgenerated option.
Run with JDK • We now have all the necessary pieces for our application. • You can open three windows and execute the following commands in a separate window in the order shown below: rmiregistry & java -DLOCATION=server rmi1.Server1Impl java -DLOCATION=client rmi1.Client1
RMI Summary • Language-dependent • Passing parameter objects supported • Easy to use