RMI - Remote Method Invocation
Terminology
- A remote object is an object whose methods may be called
by a different virtual machine than the one where the object itself lives,
generally one running on a different computer. Each remote object
implements one or more remote interfaces that declare which
methods of the remote object the foreign system can invoke.
RMI and RPC
RMI is an object-oriented implementation (written for Java) and RPC is
a procedural implementation (ie. C, Pascal) of the same communications
model:
On the OSI Model:
Transparency Issues:
- Parameter Passing
- Binding:
- finding a remote host for a desired server
- finding the correct server process on a given host
- Exception Handling
- Performance
- Security
RMI Specifics
About the Naming Registry Service:
As described in "Java Network Programming", by
Hughes, Schnoffner, and Winslow, 1997, pp.419-420:
Clients can invoke methods on remote objects only if they have a
reference to the object. A simple name server is provided in the
RMI model for this purpose. Remote objects register themselves using
the java.rmi.Naming class, which uses a URL-based scheme. An
implementation of a naming registry is supplied with the RMI distribution.
Under JDK1.1, it is provided with the rmiregistry command.
It requires no subclassing and is run before the remote object or clients
are started.
Clients use the java.rmi.Naming class to look up and obtain remote
to objects. The address of the remote object is specified with a URL that
indicates the remote machine and the name of the object on the machine.
When an object registers itself with the registry, it specifies a name by
which it can be referenced. Remote clients will connect to the registry
and locate the object based on this name.
Sun's RMI documentation
tells you everything you wanted to know about RMI and more!
Requirements
- A Remote Interface (Example:
HelloInterface.java)
- Define an interface that extends the java.rmi.Remote
interface. The Remote interface does not have any methods of its own;
its sole purpose is to tag remote objects so they can be identified as such.
One definition of a remote object is an instance of a class that implements
the Remote interface, or any interface that extends Remote.
- A remote object may have many public methods, but only those
declared in a remote interface can be invoked remotely.
- Each method in the remote interface must declare
java.rmi.RemoteException in its throws clause.
- A Remote Server-Side Object Implementation (Example:
HelloServImpl.java)
- Extend java.rmi.server.UnicastRemoteObject and implement
the remote interface(s). The UnicastRemoteObject class provides the
methods which make remote method invocation work.
- Include a constructor because you must declare that the constructor
can throw a RemoteException
- Create an instance of the RMISecurityManager. No communication
between network objects is allowed to take place without the presence of
a security manager.
- Use the Naming.rebind() method to register the object as
remote.
- The Stub and Skeleton
- Stub and skeleton objects must be generated with the rmic
tool. rmic reads the .class file of a remote object and
produces a .class "stub" file and a .class "skeleton" file.
- A Client (Example:
HelloClient.java)
- Create an instance of the RMISecurityManager.
- Use the Naming.lookup() method to establish a reference to
the remote object.
Checklist of Steps
- Define the remote interface.
- Write the server which implements the remote interface.
- Register the remote object in the name server registry.
- Generate the stub and skeleton using rmic.
- Write the client.
- Start the registry.
- Start the server.
- Run the client.