Remote Method Invocation (RMI) and Distributed Observers in Java

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

Message Passing Vs Distributed Objects
Copyright © 2003 Pearson Education, Inc. Slide 1.
UNITED NATIONS Shipment Details Report – January 2006.
Communicating in J2EE.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Chapter 24 Lists, Stacks, and Queues
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
COS 461 Fall 1997 Network Objects u first good implementation: DEC SRC Network Objects for Modula-3 u recent implementation: Java RMI (Remote Method Invocation)
DISTRIBUTED OBJECTS AND REMOTE INVOCATION
Executional Architecture
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
A1.1 Assignment 1 “Deploying a Simple Web Service” ITCS 4010/5010 Grid Computing, UNC-Charlotte B. Wilkinson, 2005.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
1 Java Remote Method Invocation java.rmi.* java.rmi.registry.* java.rmi.server.*
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.
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.
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
Changing the way of designing distributed applications Communication oriented design: FIRST design the communication protocol for the distributed system.
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.
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.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Java RMI RMI = Remote Method Invocation. Allows Java programs to invoke methods of remote objects. Only between Java programs. Several versions (JDK-1.1,
Internet Software Development Remote Method Invocation Paul Krause.
1 Java Programming II Java Network II (Distributed Objects in Java)
+ A Short Java RMI Tutorial Usman Saleem
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.
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.
Remote Method Invocation onlineTraining/rmi/RMI.html.
RMI Continued IS Outline  Review of RMI  Programming example.
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.
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.
Fall 2007cs4251 Distributed Computing Umar Kalim Dept. of Communication Systems Engineering 17/10/2007.
Java Remote Method Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
 Java RMI Distributed Systems IT332. Outline  Introduction to RMI  RMI Architecture  RMI Programming and a Sample Example:  Server-Side RMI programming.
Remote Method Invocation A Client Server Approach.
UMBC Distributed Computing with Objects RMI/Corba CMSC 432 Shon Vick.
1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Distributed programming in Java Faculty:Nguyen Ngoc Tu Session 5 - RMI.
CSC 480 Software Engineering Lab 6 – RMI Nov 8, 2002.
Netprog Java RMI1 Remote Method Invocation.
Using RMI The Example of A Remote Calculator 1. Parts of A Working RMI System A working RMI system is composed of several parts. –Interface definitions.
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
Remote Method Invocation
Chapter 40 Remote Method Invocation
Remote method invocation (RMI)
Chapter 46 Remote Method Invocation
Chapter 46 Remote Method Invocation
Java Remote Method Invocation
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Remote Method Invocation (RMI) and Distributed Observers in Java Theodore Norvell

Engineering 5895 Memorial University The Proxy Pattern The Client object uses its subject via an interface Thus it may be used with a real subject or with a proxy object which represents the subject. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University The Proxy Pattern The client calls the proxy, which forwards the call (somehow) to the actual subject. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

RMI and the Proxy pattern RMI uses the Proxy pattern to distribute objects across a network. Recall that in the Proxy pattern a proxy and a subject share a common interface. In RMI, objects call methods in a proxy (aka stub) on its own machine The proxy sends messages across the network to a “skeleton” object The skeleton calls the subject object. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University One Remote Method Call. (0) Client calls stub (1) Stub messages skeleton (2) Skeleton calls server (subject) (3) Call returns (4) Skeleton messages proxy (5) Call returns © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University Full disclosure The term “skeleton object” is out of date. The point is that some code (that you don’t have to write) running on the server’s jvm will turn network traffic into method calls to the server object and method returns (or exceptions) into network traffic. I’ll continue to use the term “skeleton object” to refer to the object(s) that perform these duties. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University Issues Concurrency If there are multiple clients, the server may field multiple calls at the same time. So use synchronization as appropriate. Argument passing Arguments are passed by value or “by proxy” not by reference. Proxy generation Proxy classes are automatically derived from the server class’s interface. Lookup Objects are usually found via a registry (program rmiregistry) © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University Nitty-Gritty The proxy and the server share an interface. This interface must extend java.rmi.Remote. Every method in the interface should be declared to throw java.rmi.RemoteException RemoteExceptions are thrown when network problems are encountered, or when server objects no longer exist. The server typically extends class java.rmi.server.UnicastRemoteObject The constructor of this class throws a RemoteException Therefore, so should the constructor of any specialization. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Argument Passing Revisited Most arguments and results are converted to a sequence of bytes; the bytes are sent over the net therefore the class should implement the java.io.Serializable interface a clone of the argument/result is constructed on the other side. The effect is pass by object value, rather than by object reference. But objects that extend java.rmi.server.RemoteObject instead have a proxy constructed for them on the other side I call this “pass by proxy”. Essentially pass by reference So each argument, result, exception type should be a primitive type, Serializable, or extend RemoteObject © 2004-10 T. S. Norvell Engineering 5895 Memorial University

An Example – Distributed Othello Othello is a two person board game My implementation uses the observer pattern so that, when the (game state) model changes, all observers are informed of the change. I wanted to put the model on one machine and the observers on other machines. Hence I implemented the observer pattern with RMI. This is example othello-2 on the website. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University The Observer Pattern Subject alerts Observers of changes of state. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University Observer Pattern © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Object diagram for Othello 2 Client Host 0 Client Host 1 :BoardView : BoardView Notifies Observes Notifies Observes :Animator :Animator Observes Observes :RemoteGameModel Notifies Notifies Server Host © 2004-10 T. S. Norvell Engineering 5895 Memorial University

… with proxies and skeletons : BoardView : BoardView Notifies Observes Notifies Observes :Animator Notifies :Animator Client Host Observes Notifies Skeleton Remote Game Model’s Proxy Skeleton Observes Communicates with Communicates with Communicates with Remote Game Model’s Proxy Another Client Host Communicates with Animator Proxy Skeleton Server Host :RemoteGameModel Animator Proxy Notifies © 2004-10 T. S. Norvell Engineering 5895 Memorial University

The Observer Pattern for Othello © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Proxy for the Remote Game Model © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Proxies for the Animators © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Animator / RemoteGameModel Relationship © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Typical sequence —slightly simplified A remote client calls a synchronized mutator on the RemoteGameModel via its stub & skeleton The RemoteGameModel updates its state and notifies each Animator via its stubs & skeletons. The Observers (Animator objects) call the RemoteGameModel accessor “getPieceAt” via its stubs and skeleton. (Therefore this accessor must not be synchronized!) getPieceAt returns The update routines return. The original mutator call returns and the RemoteGameModel becomes unlocked. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Typical sequence —slightly simplified someObj : RemoteGameModel : RemoteGameModel_Stub : Animator_Stub : Animator move(int, int, int) notifyObservers(...) update(...) Across net with help of skeleton getPieceAt(int, int) Across net with help of skeleton Calls to other accessors are similar © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Concurrent Notification The previous sequence is slightly simplified. In fact the Animators return from update immediately (so that all can be informed essentially at the same time). The Animation threads will inform the RemoteGameModel of when they have completed their animation. The RemoteGameModel waits until it has been informed that all animations are complete. The effect is that the animations can happen concurrently, yet the RemoteGameModel does not unlock until all animations are complete and all models agree on the board state. See RemotelyConcurrentlyObservable for details. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Concurrent Notification someObj : RemoteGameModel : RemoteCountMonitor : Animator move(...) notifyObservers(...) update(...) waitForZero( ) decrement( ) During this time the animators animate (using new threads created in update). The last thing these threads do is call decrement (via RMI). Once all the animation threads have called decrement, the original server thread returns from waitForZero I simplifed this diagram a bit by omitting the callbacks from the animators to the game model and by omitting the stubs and skeletons. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University Naming the Server Normally an object’s address serves as a unique identifier But this only makes sense in the context of a given JVM process. We would like objects to have unique identifiers that are unique in the world. The rmiregistry allows you to give a URI to an object And to obtain a proxy for an object that has a URI. URIs are: rmi://host/name The host must be running a rmiregistry process and that process should have the appropriate class files on its CLASSPATH © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Binding the server to a name. The main routine for the server The static method bind in Naming gives a URI to gameModel public static void main( String[] args ) { try { RemoteGameModel gameModel = new RemoteGameModel(); String name = args[0] ; Naming.bind( name, gameModel ) ; System.err.println("Game model bound to "+name);} catch( java.net.MalformedURLException e) { … } catch( AlreadyBoundException e) { … } catch( RemoteException e ) { … } } © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Bind The uri specifies the host (and port) of the RMI registry process. 1. A call Naming.bind(uri,gm) is made. i.jar only needs the class files for the server object’s interface. 1.1 A proxy object is created. Webserver 1.1.1 A serialization of the proxy is sent to the registry. 1.1.1.1 A copy is created in the registry’s vm. read Animator object i.jar get RMIRegistry GameModel object Proxy objects U is the URL of i.jar Skeleton object bind JVMs java -Djava.rmi.server.codebase=U -cp s.jar ServerMainClass gm © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Looking up the server object The clients obtain a proxy for the game model using Naming.lookup( URI ) From ClientMain.java public static void main( String[] args) { RemoteGameModelInterface proxy = null ; try { String name = args[0] ; proxy = (RemoteGameModelInterface) Naming.lookup( name ) ; } catch( java.net.MalformedURLException e) { … } catch( NotBoundException e) { … } catch( RemoteException e) { … } … continued on the slide after next … © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Looking up the server object 2. Naming.lookup(uri) is called on the client vm. java -cp c.jar ClientMainClass 2.1 A lookup message is sent to the RMI registry. lookup(uri) 2.1.1. A serialized copy of the proxy is sent to the client. And a proxy is built in the client vm. Animator object RMIRegistry GameModel object Proxy objects Skeleton object JVMs java -Djava.rmi.server.codebase=U -cp s.jar ServerMainClass © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University Closing the loop The client then can use the proxy. E.g. Continuing the ClientMain main routine … Animator animator0 = null ; try { animator0 = new Animator( proxy ) ; } catch( RemoteException e ) { … } The constructor for Animator public Animator( RemoteGameModelInterface gameModel ) throws RemoteException { super() ; gameModel.addObserver(this); } © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Adding a remote Observer. Each Animator calls addObserver(this) on the RemoteGameModel’s proxy. Since Animator implements RemoteObject, it is passed by proxy, meaning a proxy for the Animator is constructed in the JVM of the RemoteGameModel. (see next slide.) © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Adding a remote observer ani Animator object addObserver(a) GameModel object gmp addObserver( ) Proxy objects 3 the animator (ani) calls addObserver(this) on the game model’s proxy (gmp) Skeleton objects JVMs 3.1 the client vm sends a serialized proxy for ani to the server vm. anip addObserver(anip) 3.1.1 The animator’s proxy (anip) is constructed in the server vm. gm 3.1.1.1 An addObserver(anip) message is sent to the game model, which saves anip’s address to its observer list. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

The final hookup (again) Calls :Animator Skeleton Observes Communicates with Remote Game Model’s Proxy Communicates with Client Host 0 Skeleton Animator Proxy :RemoteGameModel Notifies Server Host Calls © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Creating the stubs and skeletons First we write a server class: E.g. public class RemoteGameModel extends RemotelyConcurrentlyObservable implements RemoteGameModelInterface { public RemoteGameModel() throws RemoteException { super() ; } … } We compile it with “javac” The stub objects will be created automatically at run time. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Starting an RMI registry In order to build a proxy object, the RMIRegistry process needs access to the .class file(s) for the interface. Option 0. Run the RMIRegistry with the appropriate .class files on its classpath. Option 1. Put the class files on a webserver and, when the server is run, use the following option -Djava.rmi.server.codebase=URL (Option 1 was illustrated earlier in the animation of the bind process.) © 2004-10 T. S. Norvell Engineering 5895 Memorial University

Engineering 5895 Memorial University A Few Words of Warning RMI makes it seductively easy to treat remote objects as if they were local. Keep in mind Partial Failure Part of the system of objects may fail Partial failures may be intermittent Network delays On a large network, delays are indistinguishable from failures In the Othello example, failure was not considered. The system is not designed to be resilient to partial failures. © 2004-10 T. S. Norvell Engineering 5895 Memorial University

A Few Words of Warning (cont.) Keep in mind (cont.) Performance Remote calls are several orders of magnitude more expensive than local calls (100,000 or more to 1) E.g. in the Othello example, this motivated splitting the model into local (Animator) and remote (RemoteGameModel) copies. Concurrency Remote calls introduce concurrency that may not be in a nondistributed system. E.g. in Othello, I had to be careful not to use synchronized for accessors called by remote Observers and to consider the data integrity consequences of not doing so. (The reason was that the call to the mutator and the call to the accessor are in different threads each spun off the skeleton. Therefore the call to the mutator would lock out the call to the accessor, if both were synchronized.) © 2004-10 T. S. Norvell Engineering 5895 Memorial University

A Few Words of Warning (cont.) Keep in mind (cont.) Semantics changes In Java, local calls pass objects by pointer value. Remote calls pass objects either by copy or by copying a proxy. E.g. in the Othello game as I converted from the nondistributed to a distributed version, the semantics of some calls changed, even though I did not change the source code for those calls. © 2004-10 T. S. Norvell Engineering 5895 Memorial University