Professional Documents
Culture Documents
RMI in 5 minutes
In most applications, when one object calls a method on another, both objects are on the same heap.
i.e. both are running within the same JVM.
JVM can know about references on only its own heap. JVM running in one machine cannot know
anything about different JVM running on same/different machine.
Big Pain!!
RMI comes for the rescue. With RMI you call a method on a remote object, but with code that lets you
pretend that you are invoking a method in a local object.
A design for remote method calls Create four things: server, client, skeleton (server helper), stub (client
helper).
Create client and server apps. The server app is the remote service that has an object with the method
that the client wants to invoke.
Create client and server ‘helpers’. They’ll handle all the low-level networking and I/O details so your
client and service can pretend like they’re in the same heap.
Your client object gets to act like it is making remote method calls. But what it’s really doing is calling
methods on a heap-local ‘proxy’ object that handles all the low-level details of Sockets and streams.
Client helper[stub] pretends to be the service, but it’s just a proxy for the Real Thing.
Service helper[skeleton] gets the request from the client helper, unpacks it, and calls the
method on the Real Service.
The Service object IS the Real Service. It’s the object with the method that actually does the real
method work.
Java RMI gives you the client and service helper objects! --- >>Page-614
Remote Service
Extend java.rmi.Remote and declare all methods which client need to call
Remote is a marker interface which has some special meaning for RMI.
Declare methods must throw a RemoteException
The client calls method on stub and stub does network and IO so its risky. The client has
to acknowledge the risk by handling or declaring the remote exception.
Be sure arguments and return values are primitives or Serializable
Any arguments to a remote method has to be packaged up and shipped across the
network and that is done through serialization. Same thing with return values.
import java.rmi.*;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
This return value[String] is going to be shipped over the wire from the server back to the client,
so it must be Serializable.
import java.rmi.Naming;
import J_Importent.RMI.Interface.MyRemote;
Client gets the stub object from rmi-registry using lookup method of Naming by
providing stub name [this is used by service to register stub].
The client always uses the remote implementation as the type of the
service. Programming to the IF.
Returned object need to be to cast to the interface, since the lookup
method returns type Object.
Returned stub object from RMI registry is deserialized automatically.
VVI - You must have stub object[grnerated by rmic] at client or the
stub [returned by registery] will not be deserialized, and whole
things blows up.
Client invokes a method on the stub, as though the stub IS the real service
Remember –
The client JVM needs the stub class, but the client never refers to the
stub class in code. The client always uses the remote interface, as though
the remote interface WERE the actual remote object.
Stub is required to deserialized the stub obtained from RMI-
registery.
Server needs both the Stub and Skeleton classes.
It needs the stub class because remember, the stub is substituted for
the real service, when the real service is bound to the RMI registry.
RMI Uses