1 / 25

RMI

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.

Download Presentation

RMI

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. RMI Java Remote Method Invocation

  2. Java RMI • Remote method invocation addresses the incorporation of the network into a programming language, a key issue in network computing.

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

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

  5. JAVA RMI Architecture

  6. Stubs and Skeletons Client Side Server Side Network Client m Stub Skeleton m Server

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

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

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

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

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

  12. 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”);

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

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

  15. 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);} } }

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

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

  18. Server interface package ncworld.rmi1; import java.rmi.*; public interface Server1 extends Remote { public void doSomething() throws RemoteException; }

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

  20. 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();} }

  21. 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")); }

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

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

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

  25. RMI Summary • Language-dependent • Passing parameter objects supported • Easy to use

More Related