Java RMI: Remote Method Invocation January 2000 Nancy McCracken Syracuse University.

Slides:



Advertisements
Similar presentations
Message Passing Vs Distributed Objects
Advertisements

What is RMI? Remote Method Invocation –A true distributed computing application interface for Java, written to provide easy access to objects existing.
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.
Copyright © 2001 Qusay H. Mahmoud RMI – Remote Method Invocation Introduction What is RMI? RMI System Architecture How does RMI work? Distributed Garbage.
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
Company LOGO Remote Method Invocation Georgi Cholakov, Emil Doychev, University of Plovdiv “Paisii.
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.
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
CSE331: Introduction to Networks and Security Lecture 11 Fall 2002.
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.
Java RMI RMI = Remote Method Invocation. Allows Java programs to invoke methods of remote objects. Only between Java programs. Several versions (JDK-1.1,
Java RMI Essentials Based on Mastering RMI Rickard Oberg.
1 Java Programming II Java Network II (Distributed Objects in Java)
CS 584 Lecture 18 l Assignment » Glenda assignment extended to the Java RMI Deadline » No Java RMI Assignment l Test » Friday, Saturday, Monday.
+ 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.
Presentation: RMI Continued 2 Using The Registry & Callbacks.
LAB 1CSIS04021 Briefing on Assignment One & RMI Programming February 13, 2007.
Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.
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.
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.
Presentation: RMI introduction. Goals of this lesson After this 35 min. lesson you will be: Introduced to Java RMI Ready to present RMI’s position in.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 43 Remote Method Invocation.
Java Programming: Advanced Topics 1 Networking Programming Chapter 11.
Fall 2007cs4251 Distributed Computing Umar Kalim Dept. of Communication Systems Engineering 17/10/2007.
Li Tak Sing COMPS311F. RMI callbacks In previous example, only the client can initiate a communication with the server. The server can only response to.
Java Remote Method Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
Presentation: RMI Continued 2 Using The Registry & Callbacks.
 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.
Remote Method Invocation RMI architecture stubs and skeletons for remote services RMI server and client in Java Creating an RMI Application step-by- step.
Java RMI. RMI Any object whose methods can be invoked from another Java VM is called a remote object.
Khoa CNTT 1/37 PHẠM VĂN TÍNH   Java RMI (Remote Method Invocation)
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.
RMI1 Remote Method Invocation Adapted from “Core Java 2” by Cay Horstmann.
Java Remote Method Invocation (RMI)
RMI Packages Overview java.rmi java.rmi.server java.rmi.registry
Remote Method Invocation
What is RMI? Remote Method Invocation
Remote Method Invocation
Advanced Remote Method Invocations
Java RMI (more) CS-328 Internet Programming.
Network and Distributed Programming in Java
Chapter 40 Remote Method Invocation
Remote method invocation (RMI)
Chapter 46 Remote Method Invocation
Chapter 46 Remote Method Invocation
CS 584 Lecture 18 Assignment Glenda assignment extended to the Java RMI Deadline No Java RMI Assignment Test Friday, Saturday, Monday.
Java Remote Method Invocation
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Java RMI: Remote Method Invocation January 2000 Nancy McCracken Syracuse University

RMI  Java RMI allows the programming of distributed applications across the Internet at the object level. One Java application or applet (the client in this context) can call the methods of an instance, or object, of a class of a Java application (the server in this context) running on another host machine.  An example of Distributed Object Programming - similar to CORBA, except that CORBA allows the remote objects to be programmed in other languages. –CORBA is a more general solution, but is not fully in place and has more overhead.  References: –core Java 2, Volume II - Advanced Features, Cay Horstmann and Gary Cornell, Prentice-Hall –Java RMI, Troy Bryan Downing, IDG books, –advanced Java networking, Prashant Sridharan, Sunsoft Press, –

RMI compared to other networking  In Java, it is quite easy to establish a socket network connection to another host. Communication takes place via streams. You must program protocols for message interactions and formats yourself. –High-level compared with Unix sockets, but low-level compared to RMI.  Remote Procedure Call (RPC) is a protocol to allow calling remote procedures. –Programmers register their application with a host port mapper. –Protocols for procedure parameter passing supports a limited number of primitive types.  Remote distributed objects allow objects to be passed as parameters as well. –RMI, CORBA, DCOM (for ActiveX and other Microsoft applications).

 Java RMI adds a number of classes to the Java language. The basic intent is to make a call to a remote method look and behave the same as local ones.  Important concepts –naming Registry - allows lookup to connect with remote objects –Remote interface - specification of remote methods –RemoteObjects - allows objects to be distributed –RMISecurityManager - to control the use of remote code –Serialization - protocol for representin all objects to be passed across the network. The Java RMI package Local MachineRemote Machine Local Java local method Remote Java remote method

A Remote Method Call  The architecture of a method call from the client to a method on the server. ClientServer StubsSkeletons Remote Reference Layer Transport

Stubs  To call a method on a remote machine, a surrogate method is set up for you on the local machine, called the stub.  It packages the parameters, resolving local references. This is called marshalling the parameters: –device-independent encoding of numbers –strings and objects may have local memory references and so are passed by object serialization  The stub builds an information block with –An identifier of the remote object to be used –An operation number, describing the method to be called –The marshalled parameters  Stubs will also “unmarshall” return values after the call and receive RemoteExceptions. It will throw the exceptions in the local space.

Skeletons  On the server side, a skeleton object receives the packet of information from the client stub and manages the call to the actual method: –It unmarshals the parameters. –It calls the desired method on the real remote object that lies on the server. –It captures the return value or exception of the call on the server. –It marshals that value. –It sends a package consisting of the return values and any exceptions.

Remote Reference and Transport Layers  The remote reference layer provides a Stream interface for communication between the stubs and skeletons. –It knows the local and remote objects and how to translate to the local and remote name space.  The transport layer handles all the lower-level network issues. –It sets up a connection over a physical socket. This is not necessarily TCP/IP, but may be UDP or other network protocol. –It serializes objects as required. –It monitors the connection for signs of trouble, such as the remote server doesn¹t respond, and may throw RemoteExceptions.

Local vs. Remote Objects  The goal is for local and remote objects to be semantically the same.  The most important difference between local and remote method calls is that objects are passed to local method calls effectively by reference, whereas they are copied via the serialization technique to pass to remote method calls.  For Java, an important issue is garbage collection, which automatically deallocates memory for local objects. Remote objects are also garbage collected as follows: –Remote reference layer on the server keeps reference counts for each object in Remote interface. –Remote reference layer on the client notifies the server when all references are removed for the object –When all references from all clients are removed, the server object is marked for garbage collection.

RMI Remote Interface  In setting up an RMI client and server, the starting point is the interface. This interface gives specifications of all the methods which reside on the server and are available to be called by the client.  This interface is a subclass of the Remote interface in the Java rmi package, and must be available to the compiler on both the client and server.  Example: A server whose object will have one method, sayHello(), which can be called by the client: public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }

 All remote servers are a subclass of the class UnicastRemoteObject in the rmi.server package. Extending this class means that it will be a (nonreplicated) remote object that is set up to use the default socket- based transport layer for communication.  This is the inheritance diagram of the server classes:  Beginning of example HelloImpl class: import java.rmi.*; import java.rmi.server.*; public class HelloImpl extends UnicastRemoteObject implements Hello Server Implements the remote object Object Remote Object RemoteStubRemoteServer UnicastRemoteObject Remote interface

Define the constructor for the remote object  Creating an instance of this class calls the constructor in the same way as for a normal local class. The constructor initializes instance variables of the class.  In this case, we also call the constructor of the parent class by using the keyword “super”. This call starts the server of the unicastremoteobject listening for requests on the incoming socket. Note that an implementation class must always be prepared to throw an exception if communication resources are not available. private String name;//instance variable public HelloImpl (String s) throws java.rmi.RemoteException { super(); name = s; }

Provide an implementation for each remote method  The implementation class must provide a method for each method name specified in the Remote interface. (Other methods may also be given, but they will only be available locally from other server classes.)  Note that any objects to be passed as parameters or returned as values must implement the java.io.serializable interface. Most of the core Java classes in java.lang and java.util, such as String, are serializable. public String sayHello() throws RemoteException { return “Hello, World! From” + name; }

Main method: Create an instance and install a Security Manager  This method will call the constructor of the class to create an instance.  public static void main (String args [ ]) { System.setSecurityManager (new RMISecurityManager()); try { HelloImpl obj = new HelloImpl(“HelloServer”);... // name registry code goes here } catch (Exception e) {... } // code to print exception message }

RMI Security Manager  During serialization of an object, the fields and methods are encoded by the protocol and transmitted as data. For the object to be used on the remote side, the class loader must be called to load the code for the methods of that object. Any java program that calls the class loader must have a security manager to check the classes for the security policy of that application. –For example, a security manager must make sure that any calls initiated by a remote client will not perform any “sensitive” operations, such as loading local classes.  RMISecurityManager is the default for RMI - you can write your own security manager.

Name Registry  The rmi registry is another server running on the remote machine - all RMI servers can register names with an object by calling the rebind or bind methods. Clients can then use a lookup method to find a service.  An object can be bound into a naming registry if it is a remote object –it must either extend UnicastRemoteObject or made into a remote object by calling UnicastRemoteObject.exportObject(). –Remote Objects include the stub which will be passed to the client. For this reason, when you start the rmi registry server, you must give the directory where it can find stubs of the remote object.  For large distributed applications using RMI, a design goal is to minimize the number of names in the registry. The client can obtain the name of one remote object from the registry, and other remote objects from that rmi server can be returned as values to the client. This is called “bootstrapping”.

The rmiregistry  rmiregistry is the standard naming registry service provided with Java. –You can write your own service by implementing the java.rmi.registry interface, including the Naming class methods of bind, rebind, unbind, list, lookup, etc.  The name given to rebind should be a string of the form: Naming.rebind(“//osprey7:1099/HelloServer”, obj); where the machine name can default to the current host the port number can default to the default registry port, 1099  Example call to rebind for the main method in HelloImpl: Naming.rebind(“HelloServer”, obj);  Look at full example Hello.java and HelloImpl.java

Client applet or application  The client must also have a security manager. Applets already have one; applications will make the same call to System.setSecurityManager as the server did.  The client will look up the server name in the name registry, obtaining a reference to the remote object: String url=“rmi://osprey7.npac.syr.edu/”; Hello obj = (Hello) Naming.lookup(url + “HelloServer”);  Exceptions to this call include –NotBoundException, MalformedURLException, RemoteException  Then the client can call any method in the interface to this server: obj.sayHello();  Look at Hello.html and HelloApplet.java

Summary of steps for setting up RMI  1. Compile the java code.  2. Place the interface class extending Remote on the server and the client.  3. Place the implementation class extending RemoteObject on the server.  4. Generate stubs and skeletons on the server by running the program rmic.  5. Start the name registry server on the rmi server machine.  6. Start the program that creates and registers objects of the implementation class on the rmi server machine.  7. Run the client program.

RMIC  The rmic program provided by Java takes a classfile or list of classfiles that have remote objects to export.  The options to this program include –d the directory in which to place the stubs and skeletons –show popup window to show names of methods –O optimize (same as regular compiler) –keepgenerated keeps the.java file of the generated stubs and skeletons for you to see

Passing Remote Objects  In the RMI server example, the remote object was registered in the naming registry and then passed to the client on lookup.  More generally remote objects can be passed as either input or return parameters to remote methods, enabling quite general communications patterns.  The RMI server example implemented a classic client/server model where the server provides services encapsulated into methods in a remote interface. Clients initiate communication by calling a remote method.  But the client can also provide a remote interface and pass itself as a remote object to the server. Then the server can also initiate communication by calling remote methods on the client.  In remaining pages, we sketch such a collaboration server example. We omit details about setting up threads and synchronization and the server side RMI interface, in order to show the client side interface.

Server Remote Interface  The collaboration server allows users to connect to the server. It keeps a list of users. When any user posts a message, it distributes it to all the other users.  public interface ChatServer extends Remote { //pass client object to server with id name public void register(Chat c, String name) throws RE public void postMessage ( Message m) throws RE public String[] listChatters ( ) throws RE }  Note that RE is an abbreviation for RemoteException.  The Message class must implement Serializable.

Client Remote Interface  When the server needs to distribute messages to users, it does so by calling a method in the client’s remote interface.  public interface Chat extends Remote { public void chatNotify ( Message m ) throws RE public String getName ( ) throws RE }

Server Implementation  public class ChatServerImpl extends UnicastRemoteObject implements ChatServer { private Vector chatters = new Vector(); public ChatServerImpl() throws RE {... } public void register(Chat c, String name) { chatters.addElement (... c....); } public String[ ] listChatters ( ) {... } public void postMessage ( Message m) { … c.chatNotify(Message m) …} public static void main(String[ ] args) {// set security manager, bind registry...} }

Client Implementation - Applet  public class ChatImpl extends Applet implements Chat { public ChatImpl ( ) throws RE {... } public void init ( ) {... try { UnicastRemoteObject.exportObject(this); cs = (ChatServer)Naming.lookup(...); cs.register(this, name); }...} /* other applet methods for GUI, including calls to cs.postMessage(Message m) and cs.listChatters */ public void chatNotify(Message m) throws RE { // display message } }  use rmic to create stubs and skeletons on the client side