Introduction to Remote Method Invocation (RMI)

advertisement
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
Readings
Posting on the Class Web Site
Objectives

Present the basic features of RMI

Present a simple piece of code implementing RMI
Overview of Network
Programming

Traditionally network programming has been difficult



RPC handles some of this complexity


Need to implement a communications protocol in the
application
Need to understand machine specific dependencies
Port mapping is one example
RPC permits programmer to concentrate on design
more than implementation
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
Basic Network Support in Java

Serialization permits sending primitive types as
objects



String, Float
Reference types
Can reconstruct primitive type from object
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
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
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.
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
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
Remote Object Structure

Server has skeleton class

Hands off the method calls and data to object being
referenced
Remote Object Structure
Client
Server
Stub
Skeleton
Remote Reference Layer
Transport Layer
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
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
Remote Object Structure

Layer 3 is remote reference layer

Deals with the semantics of remote invocation

Responsible for handling replicated objects
Remote Object Structure

Layer 4 is transport layer

Sets up connection between client and server

Handles transport from one machine to another
Application Layer

Application must implement a remote interface


Implementing interface is the same as any other java
interface


Extend java.rmi.Remote
Additional network based exception handling code
Export the object before use

Extend the UnicastRemoteObject class
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
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
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
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
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
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
}
An Example
Define a class that implements the remote interface. This
class extends java.rmi.UnicastRemoteObject
import java.rmi.*;
import java.rmi.server.*;
import java.net.*;
public class HelloImpl extends UnicastRemoteObject implements Hello {
Public HelloImpl() throws RemoteException {
return “Hello World!”;
}
….continued in the next slide
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);
}
}
}
An Example
1) rmic HelloImpl
Result is
Hello.class
Hello.java
HelloImpl.class
HelloImpl.java
HelloImpl_Skel.class
HelloImpl_Stub.class
An Example
Start the registry
rmiregistry 2048 & ( in unix)
start rmiregistry 2048 (in dos)
Now launch server
java HelloImpl
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);
}
}
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