Professional Documents
Culture Documents
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..
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
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
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:
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.
1. bind/rebind
2. lookup
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
>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
RMI Calculator:
RMI Calculator:
Server program
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) {
.
}