Chapter 4 Distributed objects and Remote Method Invocation
Chapter 4 Distributed objects and Remote Method Invocation Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn. 4 © Pearson Education 2005 1 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Objectives Understand how RMI works Learn the process of developing RMI applications Know the differences between RMI and socket-level programming Use callbacks to develop interactive applications 2 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Middleware layers Applications RMI, RPC and events Request reply protocol External data representation Operating System 3 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Middleware layers
Remote and local method invocations local remote invocation A B C local E invocation local invocation D remote invocation F Remote method invocation: Method invocation between objects in different processes, whether in the same computer or not Remote object reference: identifier to refer to a certain remote 4 object in a distributed system. E. g. B’s must be made available to A. Remote interface: every remote object has one that specifies which methods can be invoked remotely. E. g. B and F specify what methods in remote interface. Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All Server Liang, interface: for RPC. Server provides a set of procedure rights reserved. 0136012671
A remote object and its remote interface remoteobject Local object remote interface { Data m 1 m 2 m 3 implementation of methods m 4 m 5 m 6 • Remote interface: Class of remote object implements the methods of its remote interface. Object in other processes can only invoke methods that belong to its remote interface. However, local object can invoke remote interface methods as well as other methods implemented by remote object. • 5 Define the remote interface by extending an interface named Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Remote.
RMI Basics Java RMI is the Java Distributed Object Model for facilitating communications among distributed objects. RMI is a higher-level API built on top of sockets. Socket-level programming allows you to pass data through sockets among computers. RMI enables you not only to pass data (parameters and return values) among objects on different systems, but also to invoke methods in a remote object. 6 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
The Differences between RMI and RPC RMI is similar to Remote Procedure Calls (RPC) in the sense that both RMI and RPC enable you to invoke methods, but there are some important differences. With RPC, you call a standalone procedure. With RMI, you invoke a method within a specific object. RMI can be viewed as object-oriented RPC. 7 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
The Differences between RMI and Traditional Client/Server Approach A RMI component can act as both a client and a server, depending on the scenario in question. Peer to Peer type. A RMI system can pass functionality from a client to a server and vice versa. A client/server system typically only passes data back and forth between server and client. 8 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Distributed Object Systems/Protocols The distributed object paradigm has been widely adopted in distributed applications, for which a large number of mechanisms based on the paradigm are available. Among the most well known of such mechanisms are: ~ Java Remote Method Invocation (RMI), ~ the Common Object Request Broker Architecture (CORBA) systems, ~ the Distributed Component Object Model (DCOM), ~ mechanisms that support the Simple Object Access Protocol (SOAP). Of these, the most straightforward is the Java RMI Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
How does RMI work? An object that resides on the client host and serves as a proxy for client program and interact with skeleton. A subinterface of java. rmi. Remote that defines the methods for the server object. A program that invokes the methods in the remote server object. RMI works as follows: (1) A server object is registered with the RMI registry; (2) A client looks through the RMI registry for the remote object; (3) Once the remote object is located, its reference is returned to the client; (4) The remote object can be used in the same way as a local object. The communication between the client and the server is handled through the stub and skeleton. 10 An object that resides on the server host, communicates with the stub and the actual server object. An instance of the server object interface. A directory utility that store/registers exported remote objects and provides naming services for lookup/locating objects. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Passing Parameters When a client invokes a remote method with parameters, passing parameters are handled under the cover by the stub and the skeleton. Let us consider three types of parameters: 1. Primitive data type. A parameter of primitive type such as char, int, double, and boolean is passed by value like a local call. 11 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Passing Parameters, cont. 2. Local object type. A parameter of local object type such as java. lang. String is also passed by flatten value. This is completely different from passing object parameter in a local call. In a local call, an object parameter is passed by reference, which corresponds to the memory address of the object. In a remote call, there is no way to pass the object reference because the address on one machine is meaningless to a different Java VM. Any object can be used as a parameter in a remote call as long as the object is serializable. The stub serializes the object parameter and sends it in a stream across the network. The skeleton deserializes stream into an object. 12 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Passing Parameters, cont. 3. Remote object type. Remote objects are passed differently from the local objects. When a client invokes a remote method with a parameter of some remote object type, the reference of the remote object is passed. The server receives the reference and manipulates the parameter through the reference. 13 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
RMI Registry How does a client locate the remote object? RMI registry provides the registry services for the server to register/export the object and for the client to locate/lookup the object. A simple directory service called the RMI registry, rmiregistry, which is provided with the Java Software Development Kit (SDK). The RMI Registry is a service whose server, when active, runs on the object server’s host machine, by convention and by default on the TCP port 1099. [1] One such service is the Java Naming and Directory Interface (JNDI), which is more general than the RMI registry, in the sense that it can be used by applications that do not use the RMI API. 14 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
RMI Registry You can use several overloaded static get. Registry() methods in the Locate. Registry class to return a reference to a Registry. Once a Registry is obtained, you can bind an object with a unique name in the registry using the bind or rebind method or locate an object using the lookup method. 15 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
RMI Registry: Binding Objects 16 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Developing RMI Applications 17 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Step 1: Define Server Object Interface 1. Define a server object interface that serves as the contract between the server and its clients, as shown in the following outline: public interface Server. Interface extends Remote { public void service 1(. . . ) throws Remote. Exception; // Other methods } A server object interface must extend the java. rmi. Remote interface. 18 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Step 2: Define Server Implementation Object 2. Define a class that implements the server object interface, as shown in the following outline: public class Server. Interface. Impl extends Unicast. Remote. Object implements Server. Interface { public void service 1(. . . ) throws Remote. Exception { // Implement it } // Implement other methods } The server implementation class must extend the java. rmi. server. Unicast. Remote. Object class. The Unicast. Remote. Object class provides support for point-to-point active object references using TCP streams. 19 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Step 3: Create and Register Server Object 3. Create a server object from the server implementation class and register it with an RMI registry: Server. Interface obj= new Server. Interface. Impl(. . . ); Registry registry = Locate. Registry. get. Registry(); registry. rebind("Remote. Object. Name", obj); 20 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Step 4: Develop Client Program 4. Develop a client that locates a remote object and invokes its methods, as shown in the following outline: Registry registry = Locate. Registry. get. Registry(host); Server. Interface server = (Server. Interface. Impl) registry. lookup("Remote. Object. Name"); server. service 1(. . . ); 21 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Example: Retrieving Student Scores from an RMI Server Problem: This example creates a client that retrieves student scores from an RMI server. 22 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Step 1: Define Server Object Interface 1. Create a server interface named Student. Server. Interface. The interface tells the client how to invoke the server's find. Score method to retrieve a student score. Student. Server. Interface 23 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Step 2: Define Server Implementation Object 2. Create server implementation named Student. Server. Interface. Impl that implements Student. Server. Interface. The find. Score method returns the score for a specified student. This method returns -1 if the score is not found. Student. Server. Interface. Imp 24 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Step 3: Create and Register Server Object 3. Create a server object from the server implementation class and register it with an RMI registry. Register. With. RMIServer 25 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Step 4: Develop Client Program 4. Create a client as an applet named Student. Server. Interface. Client. The client locates the server object from the RMI registry, uses it to find the scores. Student. Server. Interface. Client 26 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Run Example 1. Start RMI Registry by typing "start rmiregistry" at a DOS prompt from the book directory. By default, the port number 1099 is used by rmiregistry. To use a different port number, simply type the command "start rmiregistry portnumber" at a DOS prompt. 2. Start Register. With. RMIServer using the following command at C: book directory: C: book>java Register. With. RMIServer 3. Run Student. Server. Interface. Client as an application. 27 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671 Start RMI Register Object with RMI Registry Run
The Server The server class is a class whose code instantiates and exports an object of the remote interface implementation. It shows a template for the object server class. import java. rmi. *; …… public class Some. Server { public static void main(String args[]) { try{ // code for port number value to be supplied Some. Impl exported. Obj = new Some. Impl(); start. Registry(RMIPort. Num); // register the object under the name “some” registry. URL = "rmi: //hostname: " + port. Num + "/some"; Naming. rebind(registry. URL, exported. Obj); System. out. println("Some Server ready. "); }// end try } // end main Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All 6/15/2021 rights reserved. 0136012671 28
The Server - 2 // This method starts a RMI registry on the local host, if it // does not already exists at the specified port number. private static void start. Registry(int RMIPort. Num) throws Remote. Exception{ try { Registry registry= Locate. Registry. get. Registry(RMIPort. Num); registry. list( ); // The above call will throw an exception // if the registry does not already exist } catch (Remote. Exception ex) { // No valid registry at that port. System. out. println( "RMI registry cannot be located at port " + RMIPort. Num); Registry registry= Locate. Registry. create. Registry(RMIPort. Num); System. out. println( "RMI registry created at port " + RMIPort. Num); } Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All } // end start. Registry rights reserved. 0136012671
The Server - 3 In our object server template, the code for exporting an object is as follows: // register the object under the name “some” registry. URL = "rmi: //hostname: " + port. Num + "/some"; Naming. rebind(registry. URL, exported. Obj); The Naming class provides methods for storing and obtaining references to objects from registry. In particular, the rebind method allow an object reference to be stored in the registry with a URL in the form of rmi: //<host name>: <port number>/<reference name> The rebind method will overwrite any reference in the registry bound with the given reference name. If the overwriting is not desirable, there is also a bind method. The host name should be the name of the server, or simply “local host”. The reference name is a name of your choice, and should be unique in the registry. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
The Server - 4 When a server is executed, the exporting of the distributed object causes the server process to begin to listen and wait for clients to connect and request the service of the object. An RMI object server is a concurrent server: each request from an object client is serviced using a separate thread of the server. Note that if a client process invokes multiple remote method calls, these calls will be executed concurrently unless provisions are made in the client process to synchronize the calls. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
The Client-side Software The program for the client class is like any other Java class. The syntax needed for RMI involves locating the RMI Registry in the server host, and looking up the remote reference for the server object; the reference can then be cast to the remote interface class and the remote methods invoked. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
The Client-side Software - 2 import java. rmi. *; …. public class Some. Client { public static void main(String args[]) { try { String registry. URL = "rmi: //hostname: " + port. Num + "/some"; Some. Interface h = (Some. Interface)Naming. lookup(registry. URL); // invoke the remote method(s) String message = h. method 1(); System. out. println(message); // method 2 can be invoked similarly } // end try catch (Exception e) { System. out. println("Exception in Some. Client: " + e); } } //end main Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All }//end class rights reserved. 0136012671
RMI Call Backs In the client-server model, the server is passive: the IPC is initiated by the client; the server waits for the arrival of requests and provides responses. Some applications require the server to initiate communication upon certain events. Examples applications are: Auctioning: user submits bid, server inform if higher bit by others. chat-room: user type message, server forwards messages from other users. message/bulletin board: One of the important benefits of RMI is that it supports callbacks, which enable the server to invoke the methods on the client. With the RMI callback feature, you can 34 develop interactive distributed applications. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Polling vs. Callback In the absence of callback, a client will have to poll a passive server repeatedly if it needs to be notified that an event has occurred at the server end. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
RMI Callbacks A callback client registers itself with an RMI server. The server makes a callback to each registered client upon the occurrence of a certain event. 36 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Interactions 37 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Remote Interface for Server public interface Hello. Interface extends Remote { // remote method public String say. Hello() throws java. rmi. Remote. Exception; // method to be invoked by a client to add itself to the callback list public void add. Callback( Hello. Callback. Interface Callback. Object) throws java. rmi. Remote. Exception; } 38 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Remote Interface for Callback Client // an interface specifying a callback method public interface Hello. Callback. Interface extends java. rmi. Remote { // method to be called by the server on callback public void call. Me ( String message ) throws java. rmi. Remote. Exception; } 39 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Hello. Server, with callback public class Hello. Server extends Unicast. Remote. Object implements Hello. Interface { static int RMIPort; // vector for store list of callback objects private static Vector callback. Objects; public Hello. Server() throws Remote. Exception { super(); // instantiate a Vector object for storing callback objects callback. Objects = new Vector(); } // method for client to call to add itself to its callback public void add. Callback( Hello. Callback. Interface Callback. Object) { // store the callback object into the vector System. out. println("Server got an 'add. Callback' call. "); callback. Objects. add. Element (Callback. Object); } 40 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Hello. Server, with callback - 2 public static void main(String args[]) { … registry = Locate. Registry. create. Registry(RMIPort); … callback( ); … } // end main private static void callback( ) { … for (int i = 0; i < callback. Objects. size(); i++) { System. out. println("Now performing the "+ i +"th callbackn"); // convert the vector object to a callback object Hello. Callback. Interface client = (Hello. Callback. Interface) callback. Objects. element. At(i); … client. call. Me ( "Server calling back to client " + i); … 41 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Hello. Client, with callback 42 Hello. Client() { // constructor System. set. Security. Manager(new RMISecurity. Manager()); // export this object as a remote object Unicast. Remote. Object. export. Object(this); // … Registry registry = Locate. Registry. get. Registry("localhost", RMIPort); h = (Hello. Interface) registry. lookup("hello"); h. add. Callback(this); // … } // end constructor // call back method - this displays the message sent by the server public void call. Me (String message) { System. out. println( "Call back received: " + message ); } public static void main(String args[]) { // … Hello. Client client = new Hello. Client(); // … while (true){ ; } // end while } // end main } // end Hello. Client class Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Example: Distributed Tic. Tac. Toe Using RMI “Distributed Tic. Tac. Toe Game, ” was developed using stream socket programming. Write a new distributed Tic. Tac. Toe game using the RMI. 43 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
Example: Distributed Tic. Tac. Toe Using RMI Tic. Tac. Toe. Interface Call. Back Tic. Tac. Toe. Impl Call. Back. Impl Tic. Tac. Toe. Client. RMI 44 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
RMI vs. Socket-Level Programming RMI enables you to program at a higher level of abstraction. It hides the details of socket server, socket, connection, and sending or receiving data. It even implements a multithreading server under the hood, whereas with socket-level programming you have to explicitly implement threads for handling multiple clients. The socket API is closely related to the operating system, and hence has less execution overhead. For applications which require high performance, this may be a consideration. RMI clients can directly invoke the server method, whereas socket-level programming is limited to passing values. Socket-level programming is very primitive. As an analogy, socket-level programming is like programming in assembly language, while RMI programming is like programming in a high-level language. 45 Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671
- Slides: 45