Title: Introduction%20to%20Remote%20Method%20Invocation%20(RMI)
1Introduction 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
2Readings
- Posting on the Class Web Site
3Objectives
- Present the basic features of RMI
- Present a simple piece of code implementing RMI
4Overview 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
5Network 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
6Basic Network Support in Java
- Serialization permits sending primitive types as
objects - String, Float
- Reference types
- Can reconstruct primitive type from object
7The RMI Architecture
- RMI is javas 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
8The RMI Architecture
- User must locate objects
- User must know how to handle exceptions related
to network communications - No IDL as in CORBA and DCOM
9The 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.
10Remote 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
11Remote 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
12Remote Object Structure
- Server has skeleton class
- Hands off the method calls and data to object
being referenced
13Remote Object Structure
Client
Server
Stub
Skeleton
Remote Reference Layer
Transport Layer
14Remote 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
15Remote 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
16Remote Object Structure
- Layer 3 is remote reference layer
- Deals with the semantics of remote invocation
- Responsible for handling replicated objects
17Remote Object Structure
- Layer 4 is transport layer
- Sets up connection between client and server
- Handles transport from one machine to another
18Application 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
19Application 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
20The 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
21The 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
22Remote 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
23Transport 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
24An 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
25An Example
Define a class that implements the remote
interface. This class extends java.rmi.UnicastRem
oteObject 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
26An Example
public static void main(String args) try
HelloImpl h new HelloImpl()
Naming.rebind(hello, h) System.out.println(Se
rver is ready) catch (RemoteException ex)
System.out.println(Exception in
HelloImpl.main ex) catch(MalformedURLEx
ception ex) System.out.println(MalformedUR
LException in HelloImpl.main ex)
27An Example
- rmic HelloImpl
- Result is
- Hello.class
- Hello.java
- HelloImpl.class
- HelloImpl.java
- HelloImpl_Skel.class
- HelloImpl_Stub.class
28An Example
Start the registry rmiregistry 2048 ( in
unix) start rmiregistry 2048 (in dos) Now
launch server java HelloImpl
29An 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)
30Summary
- 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