You are on page 1of 23

Remote Method Invocation

RMI Concepts
Two fundamental network applications
File and data transfer (FTP, SMTP (email),
HTTP, )
Allowing one host to run programs on another
host (telnet, rlogin Remote procedure call
(RPC), CGI..

Remote Method Invocation (RMI) allows


Java programs call other Java programs
on the other machines.
RMI == > distributed/parallel computing

Remote Method Invocation


The remote method invocation API let Java
objects on different hosts communicate with
each other
A remote lives on a server
Each remote object implements a remote
interface that specifies which of its methods
can be invoked by clients.
Clients invoke the methods of the remote
object almost exactly as they invoke local
methods.

Remote Method Invocation


Example:
An object running on a local client can pass
a database query as a String argument to a
method in a database object running on a
remote server to ask it to sum up a series of
records.
The server can return the result to the client
as a double.
This is more efficient than download all the
records and summing them up locally.

Remote Method Invocation


From the programmers perspective, remote
objects and methods work just like the local
objects and methods
All the implementation details are hidden.
You just
import one package,
look up the remote object in a registry and
make sure that you catch the RemoteException
when you call the objects methods

Object Serialization
When a object is passed to or returned from a Java
method, what is really transferred is a reference to
the object
Reference or object:
A special remote reference to the object is passed
A copy of the object can be passed

Reference: the local machine passes a remote


object to the remote machine
Copied object: the local machine passes its own
object (copy the object and send the copied object.)

Object Serialization
To copy an object, you need a way to
convert the object into a stream of bytes.
May not be easy: Objects may include other
objects

Object Serialization is a scheme by which


objects can be converted into a byte stream
and then passed around to other machines,
which rebuild the original objects from to bytes
These bytes can also be written to disk and
read back from disk at a later time.

Object Serialization
For security reasons, Java places some limitations
on which objects can be serialized
All Java primitive types can be serialized
Object Java objects that implement java.io.Serializable
interface

Serializable objects:

String and Component


Vector
Integer, Float (inherits from Number which is serializable)
Exceptions
Most AWT and Swing

RMI
RMI Layer model

RMI
RMI applications are often comprised of two
separate programs: a server and a client.
RMI provides the mechanism by which the
server and the client communicate and pass
information back and forth.
Such an application is sometimes referred to
as a distributed object application.

Java RMI Architecture

1. bind/rebind

2. lookup

Server Stub class rmiregistry Stub class


3. Remote Procedure Call

Client

Naming (java.rmi.Naming)
static void bind(String name, Remote obj)
Binds the specified name to a remote object.
static String[] list(String name)
Returns an array of the names bound in the
registry.
static Remote lookup(String name)
Returns a reference, a stub, for the remote
object associated with the specified name.

Naming (java.rmi.Naming)
static void rebind(String name,
Remote obj)
Rebinds the specified name to a new
remote object.
static void unbind(String name)
Destroys the binding for the specified
name that is associated with a remote
object.

RMI componets
A working RMI system is composed of
several parts:
Interface definitions for the remote services
Implementations of the remote services
Stub and Skeleton files
A server to host the remote services
An RMI Naming service that allows clients to find
the remote services
A class file provider (an HTTP or FTP server)
A client program that needs the remote services

RMI Example

RMI Calculator client/server application


Interface (define remote services):
Calculator.java
Implementation of remote services:
CalculatorImpl.java
Stub and Skeleton: CalculatorImpl_Stub.java
and CalculatorImpl_Skel.java
Server program: CalculatorServer.java
Client program: CalculatorClient.java

RMI Calculator: Interface

The Calculator interface defines all of the remote


features offered by the service:
public interface Calculator extends java.rmi.Remote {
public long add(long a, long b) throws RemoteException;
public long sub(long a, long b) throws RemoteException;
public long mul(long a, long b) throws RemoteException;
public long div(long a, long b) throws RemoteException;
}

>javac Calculator.java

RMI Calculator:
Implementation
public class CalculatorImpl extends UnicastRemoteObject
implements Calculator {
public CalculatorImpl() throws java.rmi.RemoteException
{ super(); }
public long add(long a, long b) throws RemoteException
{ return a + b; }
public long sub(long a, long b) throws RemoteException {
return a - b; }
public long mul(long a, long b) throws RemoteException
{ return a * b; }
public long div(long a, long b) throws RemoteException {
return a / b; }
}

RMI Calculator:
Implementation

The implementation class uses UnicastRemoteObject


to link into the RMI system.
This is not a requirement. A class that does not
extend UnicastRemoteObject may use its
exportObject() method to be linked into RMI.
When a class extends UnicastRemoteObject, it must
provide a constructor that declares that it may throw a
RemoteException object.
When this constructor calls super(), it activates code
in UnicastRemoteObject that performs the RMI linking
and remote object initialization.

RMI Calculator:

Stub and Skeleton

use the RMI compiler, rmic, to generate the


stub and skeleton files.
The compiler runs on the remote service
implementation class file.
>rmic CalculatorImpl
After you run rmic you should find the file
Calculator_Stub.class (both 1.1 and 1.2)
Calculator_Skel.class (only 1.1).

RMI Calculator:

Server program

Remote RMI services must be hosted in a


server process.
The class CalculatorServer is a very simple
server that provides the bare essentials for
hosting

RMI Calculator:

Server program
import java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.rebind(" rmi://localhost:1099/CalculatorService", c);
} catch (Exception e) { System.out.println("Trouble: " + e); }
}
public static void main(String args[]) {
new CalculatorServer();
}
}

RMI Calculator:

Client program
public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)
Naming.lookup( "rmi://remotehost/CalculatorService");
System.out.println( c.sub(4, 3) ); System.out.println( c.add(4,
5) ); System.out.println( c.mul(3, 6) );
System.out.println( c.div(9, 3) ); }
catch (MalformedURLException murle) {
.
}

Running RMI Applications


1. Start with the Registry.
$rmiregistry & (unix) or >start rmiregistry
(windows)
2. Start the server hosting the CalculatorService
>java CalculatorServer
3. Start the client program.
>java CalculatorClient

You might also like