Java Reference
In-Depth Information
RMI
Packages
java.rmi, java.rmi.dgc, java.rmi.registry, java.rmi.server, java.rmi.activation
Use: J2SE
Overview
RMI enables you to communicate by making remote method calls. Basically, it allows an application to run
methods on objects that are not in the same address space. This enables Java to use the same approach for
distributed communication as it does for local communications—the Java client can make a series of simple
method calls to a object. Neither the client nor the server needs to write large amounts of code to manage remote
communication in RMI—the task is handled behind the scenes by the RMI communications infrastructure.
RMI uses an interface to define the methods which can be called on the RMI server by the client. This interface
must extend java.rmi.Remote , which identifies any class that uses the interface as an RMI participant. The
interface is used by both the RMI client and server. The server implements the interface, providing the actual
functionality. The client uses the interface to identify which methods it can run remotely.
To call methods on an RMI server, a client must get a reference to the remote server object during runtime. The
client must obtain an object called a stub which enables it to locate and communicate with a specific remote
object running in some other address space. There are two ways to get a stub so that you can make remote method
calls. The most common way is to use a naming service, such as JNDI or the rmiregistry. Naming services allow
clients to perform lookup operations for remote objects, and to obtain stubs for remote communication. Another
way to obtain a stub is to make a remote method call on an object which itself returns a stub.
The stub on the client is responsible for communicating with the remote object on the server. It greatly simplifies
the task of remote communication for the client. In client code, a remote method call looks exactly like an
ordinary method call. When a client application makes a remote method call, the stub receives the call,
communicates with the server, and returns the result to the client.
The stub marshalls arguments used in methods, and the stub unmarshalls the return value before returning it to the
client. Marshalling is a specialized form of serialization, where the object is wrapped in a
java.rmi.MarshalledObject . The MarshalledObject contains the serialized object URI where the class file of
the send object can be located, plus codebase, annotated with a codebase property. The codebase is a String
representation of the URL where the object's class file is located.
Creating an RMI application is not complicated. The first thing to do is define the remote interface; that is, the
interface that determines what methods are available remotely. It has to extend java.rmi.Remote and all methods
have to declare throwing at least a java.rmi.RemoteException.
The remote object implements the remote interface however it sees fit. When a remote object is send across its
address space, not the remote object is send, but the stub is. To accomplish this the remote object has to “export”
itself, which means that the JVM has to be notified that this is a remote object and it should behave accordingly.
An unexported remote object sent across the wire behaves like any other regular (non-remote) object and gets
copied across the network. This can lead to unexpected behaviour. There are two ways to export a remote object.
The first is to extend java.rmi.server.UnicastRemoteObject. In the constructor of this class, the object is exported.
The other way to export the remote object is to explicitly call the exportObject method in UnicastRemoteObject.
Then the remote object is not required to extend UnicastRemoteObject.
To make the remote object available to other objects, the remote object can register itself with a name service.
The client looks up a remote object through a name service. It gets an object back, which it can cast to the
expected type (remote interface) tied to the name used in the request. From there on, the client uses that object to
invoke methods on. The stub forwards the calls to the remote object and waits for the return value. The stub may
throw java.rmi.RemoteException to signal that there was a problem in communicating with the remote object.
The stub can be automatically generated by the rmic tool.
 
Search WWH ::




Custom Search