Intro to RMI
- RMI: Java Remote Method Invication
- Provides remote communication between programs written
in Java
- Method calls between JVM 1 and JVM 2
Overview
- RMI connection needs 1) client, 2) server and 3) remote
object
- Server app:
- Creates remote objects
- Makes references to them accessible
- Waits clients to invoke the remote object's
methods
- Client app:
- Gets reference to the remote object
- Invokes methods on remote objects
Locating and Communicating
- To locate the remote object,
rmiregistry is used
- Rmiregistry is a simple naming facility which is used
to locate remoteobjects
- Provides remote communication between programs written
in Java
- Communicating looks like normal method
invication!
Locating and Communicating
- To locate the remote object,
rmiregistry is used
- Rmiregistry is a simple naming facility which is used
to locate remoteobjects
- Provides remote communication between programs written
in Java
- Communicating looks like normal method
invication!
RMI Overview
Implementing RMI System
- Client locates remote object from rmiregistry and
invokes remote object's methods.
- Server binds remote object to rmiregistry
- Remote object implements Remote
interface
Class Diagram of the Server - side
UnicastRemoteObject and Remote - interface
- RemoteObject must:
- inherit
java.rmi.server.UnicastRemoteObject
- implement interface that inherites
java.rmi.Remote
java.rmi.Remote interface does not have
any methods!
- Every remote method must throw
java.rmi.RemoteException
Parameter passing
- Java: Every object is passed by reference
- Exception! When calling remote method, object's are
passed by copy
- Parameter must be serializable. Object
is transferred to bits and moved from one JVM to
another
Stub and Skeleton
- Stub and Skeleton: classes that are used to create the
real communication between client and server.
-
Stub class: acts as client's
representive for the remote object
- Initiative the connection, marshals the parameter,
waits result, unmarhals return value, return the value
to the caller
-
Skeleton class: Each remote object has
corresponding skeleton class
- unmarhals the parameter, invokes the method,
marhals the result to the caller
- Stub and Skeleton classes are created by using
rmic compiler.
- The creation of stub and skeleton are not needed in
Java 5 or later
In Practice: Creating a simple RMI system
- Define a interface that holds remote
methods. Remember that every method must throw
java.rmi.RemoteException and the interface
must inherit java.rmi.Remote
- Create RemoteObject class that
implements the interface implemented above. Remember that
this class must inherit
java.rmi.UnicastRemoteObject and it's default
constructor must throw
java.rmi.RemoteException
- Create Server class that creates the
RemoteObject and binds it to the rmiregistry
- Create a client that locates
RemoteObject from rmiregistry and calls it's remote
methods
- (Optional) create stub and skeleton classes using
rmic compiler
- Start rmiregistry, server and
client
1) Define interface
import java.rmi.*;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
2) Create Remote Object
import java.rmi.*;
import java.rmi.server.*;
public class RemoteObject extends UnicastRemoteObject
implements Hello {
public RemoteObject() throws RemoteException { }
public String sayHello() throws RemoteException {
return "Hello from another JVM!";
}
}
3) Create Server
import java.rmi.*;
import java.rmi.server.*;
public class HelloServer {
public static void main(String [] args) {
try {
RemoteObject remoteobject = new RemoteObject();
Naming.rebind("donaldduck", remoteobject);
System.out.println("Remote object is now
bind to the rmi-registry");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
4) Create Client
import java.rmi.*;
import java.rmi.server.*;
public class HelloClient {
public static void main(String [] args) {
try {
Hello external =
(Hello)Naming.lookup("//localhost/donaldduck");
String message = external.sayHello();
System.out.println(message);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
5) Create Stub and Skeleton classes
> rmic -v1.1 RemoteObject
// creates RemoteObject_Skel.class and RemoteObject_Stub.class
6) Start the application
> rmiregistry
> java HelloServer
> java HelloClient