Presentation is loading. Please wait.

Presentation is loading. Please wait.

Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Similar presentations

Presentation on theme: "Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI."— Presentation transcript:

1 Remote Method Invocation Chin-Chih Chang

2 Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI. –Platform-dependent objects such as file descriptors and sockets cannot be serialized. During an RMI local objects are passed by object copying whereas remote objects are passed by reference. A remote object is built from two different classes: –Server class – implementation of server-side code. –Client class – implementation of a proxy. In Java, a proxy is serializable and is used as a reference to the remote object. –It is possible to marshal a proxy and send it as a series of bytes to another process. –Passing proxies as parameters works because each process is executing in the same Java virtual machine.

3 Remote Method Invocation (RMI) RMI TCP/IP stack Application network The Java Remote Method Invocation (RMI) system allows an object running in one Java Virtual Machine (VM) to invoke methods on an object running in another Java VM. Java RMI provides applications with transparent andlightweight access to remote objects. RMI defines a high-level protocol and API. Programming distributed applications in Java RMI is simple.  It is a single-language system.  The programmer of a remote object must consider its behavior in a concurrent environment.

4 Java RMI A Java RMI application is referred to as a distributed object application which needs to: –Locate remote objects: Applications can use one of two mechanisms to obtain references to remote objects. An application can register its remote objects with RMI's simple naming facility, the rmiregistry, or the application can pass and return remote object references as part of its normal operation. –Communicate with remote objects: Details of communication between remote objects are handled by RMI; to the programmer, remote communication looks like a standard Java method invocation. –Load class bytecodes for objects that are passed around: Because RMI allows a caller to pass objects to remote objects, RMI provides the necessary mechanisms for loading an object's code, as well as for transmitting its data.

5 Java RMI Java RMI extends the Java object model to provide support for distributed objects in the Java language. It allows objects to invoke methods on remote objects using the same syntax as for local invocations. Type checking applies equally to remote invocations as to local ones. The remote invocation is known because RemoteExceptions has been handled and the remote object is implemented using the Remote interface. The semantics of parameter passing is different from the local invocation because the invoker and the target reside on different machines.

6 RMI Implementation Java Virtual Machine Client Object Java Virtual Machine Remote Object StubSkeleton Client Host Server Host

7 Remote References and Interfaces Remote References –Refer to remote objects, but can be invoked on a client just like a local object reference Remote Interfaces –Declare exposed methods like an RPC specification –Implemented on the client like a proxy for the remote object

8 Stubs and Skeletons Client Stub –lives on the client –pretends to be the remote object Sever Skeleton –lives on the server –receives requests from the stub –talks to the true remote object –delivers the response to the stub

9 Remote Interface Stub Remote Object (Server) ClientSkeleton implements Remote Interface

10 RMI Implementation Reference Layer – determines if referenced object is local or remote. Transport Layer – packages remote invocations, dispatches messages between stub and skeleton, and handles distributed garbage collection. Both layers reside on top of

11 RMI Registry the RMI registry is a simple server-side bootstrap naming facility that allows remote clients to get a reference to a remote object Servers name and register their objects to be accessed remotely with the RMI Registry. Clients use the name to find server objects and obtain a remote reference to those objects from the RMI Registry. A registry (using the rmiregistry command) is a separate process running on the server machine.

12 Registry RMI Registry Architecture Java Virtual Machine Client Java Virtual Machine Stub Remote Object Skeleton Java Virtual Machine “Bob” Server

13 Creating Distributed Applications Using RMI Steps to create a RMI application: 1.Design and implement the components of your distributed application. 2.Compile sources and generate stubs. 3.Make classes network accessible. 4.Start the application.

14 Design and Implement the Application Components This step includes: –Defining the remote interfaces: A remote interface specifies the methods that can be invoked remotely by a client. –Implementing the remote objects: Remote objects must implement one or more remote interfaces. The remote object class may include implementations of other interfaces (either local or remote) and other methods (which are available only locally). If any local classes are to be used as parameters or return values to any of these methods, they must be implemented as well. –Implementing the clients: Clients that use remote objects can be implemented at any time after the remote interfaces are defined, including after the remote objects have been deployed.

15 Compile Sources and Generate Stubs This step includes: –Use the javac compiler to compile the source files, which contain the implementation of the remote interfaces and implementations, the server classes, and the client classes. –Use the rmic compiler to create stubs for the remote objects. RMI uses a remote object's stub class as a proxy in clients so that clients can communicate with a particular remote object.

16 Make Classes Network Accessible and Start the Application Make Classes Network Accessible: –In this step you make everything - the class files associated with the remote interfaces, stubs, and other classes that need to be downloaded to clients - accessible via a Web server. Start the Application –Starting the application includes running the RMI remote object registry (rmiregistry), the server, and the client.

17 Java RMI - Example The files needed for creating a Java RMI application are:  A remote interface defines the remote interface provided by the service. Usually, it is a single line statement specifies the service function ( (An interface is the skeleton for a public class.)  A remote object implements the remote service. It contains a constructor and required functions. (  A client that invokes the remote method. (  The server offers the remote service, installs a security manager and contacts rmiregistry with an instance of the service under the name of the remote object. (

18 import java.rmi.*; public interface HelloInterface extends Remote { public String say(String msg) throws RemoteException; }

19 import java.rmi.*; import java.rmi.server.*; public class Hello extends UnicastRemoteObject implements HelloInterface { private String message; public Hello(String msg) throws RemoteException { message = msg; } public String say(String m) throws RemoteException { // return input message - reversing input and suffixing // our standard message return new StringBuffer(m).reverse().toString() + "\n" + message; }

20 import java.rmi.*; public class HelloClient { public static void main(String args[]) { String path = "//localhost/Hello"; try { if (args.length < 1) { System.out.println("usage: java HelloClient... \n"); } else path = "//" + args[0] + "/Hello"; HelloInterface hello = (HelloInterface) Naming.lookup(path); for (int i = 0; i < args.length; ++i) System.out.println(hello.say(args[i])); } catch(Exception e) { System.out.println("HelloClient exception: " + e); }

21 import java.rmi.*; import java.rmi.server.*; public class HelloServer { public static void main(String args[]) { // Create and install a security manager if (System.getSecurityManager() == null) System.setSecurityManager(new RMISecurityManager()); try { Naming.rebind("Hello", new Hello("Hello, world!")); System.out.println("server is running..."); } catch (Exception e) { System.out.println("Hello server failed:" + e.getMessage()); }

22 Steps to Build a RMI application - Example Write the following Java code: Interface - Implementation class – Client – Server – Compile the code javac Generate Stub and Skeleton class files from the implementation classes (make sure that your classpath contains your current directory) rmic Hello

23 Steps to Build a RMI application - Example Start the RMI registry (in a separate window or in the background) rmiregistry & (be sure to kill this process when you're done) Start the server in one window or in the background with the security policy java HelloServer or without the security policy java HelloServer Run the client in another window java HelloClient testing

24 Registry RMI Steps Java Virtual Machine Client Java Virtual Machine Stub Remote Object Skeleton Java Virtual Machine “Bob” Server Server creates and registers remote object Client obtains a remote reference from the registry, and a stub is created

Download ppt "Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI."

Similar presentations

Ads by Google