Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Remote Method Invocation (RMI)

Similar presentations

Presentation on theme: "Introduction to Remote Method Invocation (RMI)"— Presentation transcript:

1 Introduction to Remote Method Invocation (RMI)
Organizational Communications and Technologies Prithvi N. Rao H. John Heinz III School of Public Policy and Management Carnegie Mellon University

2 Posting on the Class Web Site
Readings Posting on the Class Web Site

3 Objectives Present the basic features of RMI
Present a simple piece of code implementing RMI

4 Overview of Network Programming
Traditionally network programming has been difficult Need to implement a communications protocol in the application Need to understand machine specific dependencies RPC handles some of this complexity Port mapping is one example RPC permits programmer to concentrate on design more than implementation

5 Network Support in Java
Support for TCP and UDP sockets is provided Developer still needs to worry about the packing and unpacking of data RPC falls short with object oriented code because there is no simple way to represent objects Methods take objects as parameters Return values are often objects RMI uses object serialization to accomplish sending objects as parameters and objects as return values

6 Basic Network Support in Java
Serialization permits sending primitive types as objects String, Float Reference types Can reconstruct primitive type from object

7 The RMI Architecture RMI is java’s answer to RPC
Also true for CORBA and DCOM RMI can be deployed on virtual machines on the same platform or across a network RMI provides a high level interface for building applications Aim is to facilitate the development of distributed applications as easily as non-distributed applications

8 The RMI Architecture User must locate objects
User must know how to handle exceptions related to network communications No IDL as in CORBA and DCOM

9 The RMI Architecture Differences exist between local object and remote object invocation Object passed as a parameter in remote case must be serializable or another Remote object. Objects passed as parameters or values returned from methods must be passed by value not reference. Client always refers to remote object via one of the remote interfaces it implements.

10 Remote Object Structure
Remote Method Invocation is made through reference to a remote object. Object is exported via a server application Handle to object is obtained by remote client Looks up registry Checking return value from another remote method call Object must implement at least one interface that extends the java.rmi.Remote interface

11 Remote Object Structure
Reference to object is not sent over network to client requesting it Client uses a proxy for the object All interaction is done via proxy Each client has a stub for the remote object but there is only one remote object Can have many clients each with their stubs

12 Remote Object Structure
Server has skeleton class Hands off the method calls and data to object being referenced

13 Remote Object Structure
Client Server Stub Skeleton Remote Reference Layer Transport Layer

14 Remote Object Structure
Layer1 is the application layer Actual implementation of the client and server applications High level calls are made to access and export remote objects

15 Remote Object Structure
Layer 2 is the proxy layer or stub/skeleton layer Applications deal with this layer directly All calls to remote methods and marshalling of parameters and return objects done using proxies

16 Remote Object Structure
Layer 3 is remote reference layer Deals with the semantics of remote invocation Responsible for handling replicated objects

17 Remote Object Structure
Layer 4 is transport layer Sets up connection between client and server Handles transport from one machine to another

18 Application Layer Application must implement a remote interface
Extend java.rmi.Remote Implementing interface is the same as any other java interface Additional network based exception handling code Export the object before use Extend the UnicastRemoteObject class

19 Application Layer Register application with a name server or registry
Name service is only necessary at startup Client requests remote object from either a registry or remote object already obtained

20 The Stub Class Generated using the rmic compiler
Stub is client side proxy for remote object Responsible for initiating call to remote object Stub responsible for marshaling method arguments

21 The Skeleton Class Also responsible for marshaling parameters
Skeleton is on the server side Receives method calls from the client stubs Dispatches method calls to the server remote interface implementation

22 Remote Reference Layer
Abstraction between stub and skeleton classes Handles replicated objects Replicated objects allow simple dispatch to many programs exporting the same interface Establishes persistence semantics and strategies for recovery of lost connections

23 Transport Layer Handles machine-to-machine communication
Default communication is done via TCP/IP Can be modified to handle encrypted streams, compression algorithms and security and other performance related enhancements Application layers void of this detail

24 An Example In the server first extend the java.rmi.Remote interface
import java.rmi.*; public interface Hello extends Remote { public String myHello() throws java.rmi.RemoteException }

25 An Example Define a class that implements the remote interface. This
class extends java.rmi.UnicastRemoteObject import java.rmi.*; import java.rmi.server.*; import*; public class HelloImpl extends UnicastRemoteObject implements Hello { Public HelloImpl() throws RemoteException { return “Hello World!”; } ….continued in the next slide

26 An Example public static void main(String args[]) { try {
HelloImpl h = new HelloImpl(); Naming.rebind(“hello”, h); System.out.println(“Server is ready”); } catch (RemoteException ex) { System.out.println(“Exception in HelloImpl.main” + ex); } catch(MalformedURLException ex) { System.out.println(“MalformedURLException in HelloImpl.main” + ex); }

27 An Example rmic HelloImpl Result is Hello.class
HelloImpl.class HelloImpl_Skel.class HelloImpl_Stub.class

28 An Example Start the registry rmiregistry 2048 & ( in unix)
start rmiregistry 2048 (in dos) Now launch server java HelloImpl

29 An Example: Client Side
import java.rmi.*; public class HelloClient { public static void main(String args[]) { System.setSecurityManager(new RMISecurityManager()); try { Hello h = (Hello) Naming.lookup(“hello”); String message = h.myHello(); System.out.println(“HelloClient: “ + message); } catch (Exception ex) { System.out.println(“Exception in main: “ + ex);

30 Summary RMI is alternative to CORBA and DCOM
Aim is to make writing distributed java applications as simple as non distributed java applications Use of registry to register server Use of stubs and skeletons

Download ppt "Introduction to Remote Method Invocation (RMI)"

Similar presentations

Ads by Google