Chapter 4 Distributed objects and Remote Method Invocation

Slides:



Advertisements
Similar presentations
Message Passing Vs Distributed Objects
Advertisements

RMI Varun SainiYing Chen. What is RMI? RMI is the action of invoking a method of a remote interface on a remote object. It is used to develop applications.
Remote Method Invocation (RMI) Mixing RMI and sockets
Remote Method Invocation CS587x Lecture Department of Computer Science Iowa State University.
15-May-15 RMI Remote Method Invocation. 2 “The network is the computer” Consider the following program organization: If the network is the computer, we.
Remote Method Invocation in Java Bennie Lewis EEL 6897.
Advanced Programming Rabie A. Ramadan Lecture 4. A Simple Use of Java Remote Method Invocation (RMI) 2.
Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme.
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Remote Method Invocation
Remote Object Invocation Tran, Van Hoai Department of Systems & Networking Faculty of Computer Science & Engineering HCMC University of Technology.
6 Maart 2006ISS Distributed Objects and Advanced Remote Method Invocations René de Vries Based on the slides by M.L. Liu and Marko van Eekelen.
FONG CHAN SING (143334) WONG YEW JOON (143388). JAVA RMI is a distributive system programming interface introduced in JDK 1.1. A library that allows an.
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.
Comparison of the RMI and the socket APIs
Sockets  Defined as an “endpoint for communication.”  Concatenation of IP address + port.  Used for server-client communication.  Server waits for.
EEC-681/781 Distributed Computing Systems Lecture 5 Wenbing Zhao Department of Electrical and Computer Engineering Cleveland State University
Introduction to Remote Method Invocation (RMI)
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Practical Issues of RPCCS-4513, D-Term Remote Procedure Call Practical Issues CS-4513 Distributed Computing Systems (Slides include materials from.
Communication in Distributed Systems –Part 2
Lesson 3 Remote Method Invocation (RMI) Mixing RMI and sockets Rethinking out tic-tac-toe game.
Slides for Chapter 5: Communication between distributed objects
1 Java Programming II Java Network II (Distributed Objects in Java)
15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using.
11 September 2008CIS 340 # 1 Topics To examine the variety of approaches to handle the middle- interaction (continued) 1.RPC-based systems 2.TP monitors.
Java RMI: Remote Method Invocation January 2000 Nancy McCracken Syracuse University.
1 Java RMI G53ACC Chris Greenhalgh. 2 Contents l Java RMI overview l A Java RMI example –Overview –Walk-through l Implementation notes –Argument passing.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Java Remote Method Invocation RMI. Idea If objects communicate with each other on one JVM why not do the same on several JVM’s? If objects communicate.
Systems Prog. & Script. - Heriot Watt Univ 1 Systems Programming & Scripting Lecture 11: The Distributed Object Model.
RMI remote method invocation. Traditional network programming The client program sends data to the server in some intermediary format and the server has.
RMI Remote Method Invocation Distributed Object-based System and RPC Together 2-Jun-16.
Presentation: RMI Continued 2 Using The Registry & Callbacks.
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
 Remote Method Invocation  A true distributed computing application interface for Java, written to provide easy access to objects existing on remote.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 43 Remote Method Invocation.
11/29/20151 Distributed Objects. 11/29/20152 Message Passing vs. Distributed Objects.
Fall 2007cs4251 Distributed Computing Umar Kalim Dept. of Communication Systems Engineering 17/10/2007.
Chapter 5: Distributed objects and remote invocation Introduction Remote procedure call Events and notifications.
Java Remote Method Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Presentation: RMI Continued 2 Using The Registry & Callbacks.
DISTRIBUTED OBJECTS AND REMOTE INVOCATION 1. 2 Topics  Middleware  Remote Method Invocation  Remote Procedure Call.
 Java RMI Distributed Systems IT332. Outline  Introduction to RMI  RMI Architecture  RMI Programming and a Sample Example:  Server-Side RMI programming.
Slides for Chapter 5: Distributed objects and remote invocation From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition.
Remote Method Invocation A Client Server Approach.
UMBC Distributed Computing with Objects RMI/Corba CMSC 432 Shon Vick.
Remote Method Invocation RMI architecture stubs and skeletons for remote services RMI server and client in Java Creating an RMI Application step-by- step.
1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 29 Remote Method.
Distributed Web Systems Distributed Objects and Remote Method Invocation Lecturer Department University.
Java Remote Method Invocation (RMI)
Remote Method Invocation
What is RMI? Remote Method Invocation
Remote Method Invocation
Advanced Remote Method Invocations
Edition 5, © Addison-Wesley 2012
Java RMI (more) CS-328 Internet Programming.
Network and Distributed Programming in Java
Lecture 4: RPC Remote Procedure Call Coulouris et al: Chapter 5
Chapter 40 Remote Method Invocation
Slides for Chapter 5: Distributed objects and remote invocation
Lecture 4: RPC Remote Procedure Call CDK: Chapter 5
Remote method invocation (RMI)
Creating a Distributed System with Remote Procedure Calls and Web Services Ch.5 B.Ramamurthy 2/17/2019 B.Ramamurthy.
Remote Method Invocation
Chapter 46 Remote Method Invocation
Chapter 46 Remote Method Invocation
Java Remote Method Invocation
Presentation transcript:

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

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

Middleware layers Applications RMI, RPC and events Middleware Request reply protocol External data representation Operating System RMI, RPC and events

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. 4 © Pearson Education 2005

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.

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.

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.

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.

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

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.

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.

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.

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.

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.

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.

RMI Registry: Binding Objects

Developing RMI Applications

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.

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.

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);

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

Example: Retrieving Student Scores from an RMI Server Problem: This example creates a client that retrieves student scores from an RMI server.

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

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

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

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

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

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

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

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.

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.

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.

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  

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.

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.

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.

Callback Client-Server Interactions

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; }

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; }

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);

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);

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

Example: Distributed TicTacToe Using RMI “Distributed TicTacToe Game,” was developed using stream socket programming. Write a new distributed TicTacToe game using the RMI.

Example: Distributed TicTacToe Using RMI TicTacToeInterface CallBack TicTacToeImpl CallBackImpl TicTacToeClientRMI

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.