Download presentation
Published byHector Young Modified over 9 years ago
1
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
2
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
3
Middleware layers Applications RMI, RPC and events Middleware
Request reply protocol External data representation Operating System RMI, RPC and events
4
Remote and local method invocations
B C D E 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 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. Server interface: for RPC. Server provides a set of procedure that are available for use by client. File server provide reading and writing files. Instructor’s Guide for Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edn © Pearson Education 2005
5
A remote object and its remote interface
Data implementation object { of methods Local object 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. Define the remote interface by extending an interface named Remote.
6
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.
7
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.
8
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.
9
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
10
How does RMI work? A subinterface of java.rmi.Remote that defines the methods for the server object. An object that resides on the server host, communicates with the stub and the actual server object. An object that resides on the client host and serves as a proxy for client program and interact with skeleton. An instance of the server object interface. A program that invokes the methods in the remote server object. A directory utility that store/registers exported remote objects and provides naming services for lookup/locating objects. 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.
11
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.
12
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.
13
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.
14
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.
15
RMI Registry You can use several overloaded static getRegistry() methods in the LocateRegistry 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.
16
RMI Registry: Binding Objects
17
Developing RMI Applications
18
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 ServerInterface extends Remote { public void service1(...) throws RemoteException; // Other methods } A server object interface must extend the java.rmi.Remote interface.
19
Step 2: Define Server Implementation Object
2. Define a class that implements the server object interface, as shown in the following outline: public class ServerInterfaceImpl extends UnicastRemoteObject implements ServerInterface { public void service1(...) throws RemoteException { // Implement it } // Implement other methods The server implementation class must extend the java.rmi.server.UnicastRemoteObject class. The UnicastRemoteObject class provides support for point-to-point active object references using TCP streams.
20
Step 3: Create and Register Server Object
3. Create a server object from the server implementation class and register it with an RMI registry: ServerInterface obj= new ServerInterfaceImpl(...); Registry registry = LocateRegistry.getRegistry(); registry.rebind("RemoteObjectName", obj);
21
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 = LocateRegistry.getRegistry(host); ServerInterface server = (ServerInterfaceImpl) registry.lookup("RemoteObjectName"); server.service1(...);
22
Example: Retrieving Student Scores from an RMI Server
Problem: This example creates a client that retrieves student scores from an RMI server.
23
Step 1: Define Server Object Interface
1. Create a server interface named StudentServerInterface. The interface tells the client how to invoke the server's findScore method to retrieve a student score. StudentServerInterface
24
Step 2: Define Server Implementation Object
2. Create server implementation named StudentServerInterfaceImpl that implements StudentServerInterface. The findScore method returns the score for a specified student. This method returns -1 if the score is not found. StudentServerInterfaceImp
25
Step 3: Create and Register Server Object
3. Create a server object from the server implementation class and register it with an RMI registry. RegisterWithRMIServer
26
Step 4: Develop Client Program
4. Create a client as an applet named StudentServerInterfaceClient. The client locates the server object from the RMI registry, uses it to find the scores. StudentServerInterfaceClient
27
Run Example Run Start RMI
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. Start RMI 2. Start RegisterWithRMIServer using the following command at C:\book directory: C:\book>java RegisterWithRMIServer Register Object with RMI Registry 3. Run StudentServerInterfaceClient as an application. Run
28
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 SomeServer { public static void main(String args[]) { try{ // code for port number value to be supplied SomeImpl exportedObj = new SomeImpl(); startRegistry(RMIPortNum); // register the object under the name “some” registryURL = "rmi://hostname:" + portNum + "/some"; Naming.rebind(registryURL, exportedObj); System.out.println("Some Server ready."); }// end try } // end main 4/22/2017
29
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 startRegistry(int RMIPortNum) throws RemoteException{ try { Registry registry= LocateRegistry.getRegistry(RMIPortNum); registry.list( ); // The above call will throw an exception // if the registry does not already exist } catch (RemoteException ex) { // No valid registry at that port. System.out.println( "RMI registry cannot be located at port " + RMIPortNum); Registry registry= LocateRegistry.createRegistry(RMIPortNum); "RMI registry created at port " + RMIPortNum); } // end startRegistry
30
The Server - 3 In our object server template, the code for exporting an object is as follows: // register the object under the name “some” registryURL = "rmi://hostname:" + portNum + "/some"; Naming.rebind(registryURL, exportedObj); 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.
31
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.
32
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.
33
The Client-side Software - 2
import java.rmi.*; …. public class SomeClient { public static void main(String args[]) { try { String registryURL = "rmi://hostname:" + portNum + "/some"; SomeInterface h = (SomeInterface)Naming.lookup(registryURL); // invoke the remote method(s) String message = h.method1(); System.out.println(message); // method2 can be invoked similarly } // end try catch (Exception e) { System.out.println("Exception in SomeClient: " + e); } } //end main }//end class
34
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 develop interactive distributed applications.
35
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.
36
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.
37
Callback Client-Server Interactions
38
Remote Interface for Server
public interface HelloInterface extends Remote { // remote method public String sayHello() throws java.rmi.RemoteException; // method to be invoked by a client to add itself to the callback list public void addCallback( HelloCallbackInterface CallbackObject) throws java.rmi.RemoteException; }
39
Remote Interface for Callback Client
// an interface specifying a callback method public interface HelloCallbackInterface extends java.rmi.Remote { // method to be called by the server on callback public void callMe ( String message ) throws java.rmi.RemoteException; }
40
HelloServer, with callback
public class HelloServer extends UnicastRemoteObject implements HelloInterface { static int RMIPort; // vector for store list of callback objects private static Vector callbackObjects; public HelloServer() throws RemoteException { super(); // instantiate a Vector object for storing callback objects callbackObjects = new Vector(); } // method for client to call to add itself to its callback public void addCallback( HelloCallbackInterface CallbackObject) { // store the callback object into the vector System.out.println("Server got an 'addCallback' call."); callbackObjects.addElement (CallbackObject);
41
HelloServer, with callback - 2
public static void main(String args[]) { … registry = LocateRegistry.createRegistry(RMIPort); callback( ); } // end main private static void callback( ) { for (int i = 0; i < callbackObjects.size(); i++) { System.out.println("Now performing the "+ i +"th callback\n"); // convert the vector object to a callback object HelloCallbackInterface client = (HelloCallbackInterface) callbackObjects.elementAt(i); client.callMe ( "Server calling back to client " + i);
42
HelloClient, with callback
HelloClient() { // constructor System.setSecurityManager(new RMISecurityManager()); // export this object as a remote object UnicastRemoteObject.exportObject(this); // … Registry registry = LocateRegistry.getRegistry("localhost", RMIPort); h = (HelloInterface) registry.lookup("hello"); h.addCallback(this); // … } // end constructor // call back method - this displays the message sent by the server public void callMe (String message) { System.out.println( "Call back received: " + message ); } public static void main(String args[]) { // … HelloClient client = new HelloClient(); // … while (true){ ; } // end while } // end main } // end HelloClient class
43
Example: Distributed TicTacToe Using RMI
“Distributed TicTacToe Game,” was developed using stream socket programming. Write a new distributed TicTacToe game using the RMI.
44
Example: Distributed TicTacToe Using RMI
TicTacToeInterface CallBack TicTacToeImpl CallBackImpl TicTacToeClientRMI
45
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.