Java Reference
In-Depth Information
25.6.
java.rmi
Remote Method Invocation
When you can download and run code on other systems, the face of
distributed computing changes. The Java platform's Remote Method In-
vocation (
RMI
) gives you a way to create objects whose methods can
be invoked from other virtual machines, including those running on
completely different hosts. Because
RMI
is designed for communicating
between Java virtual machines it can take advantage of the architecture's
features and can operate in a natural fashion for programmers.
RMI
is
contained in the package
java.rmi
and its subpackages, most notably the
package
java.rmi.server
for
RMI
server implementations.
To use
RMI
you must first design one or more
remote interfaces
interfaces
whose methods can be invoked remotely. A remote interface extends the
Remote
interface, and its methods throw
RemoteException
in addition to any
other exceptions. Here, for example, is a simple definition of a compute
server interface that will accept
Task
objects for execution, returning the
resulting
Object
:
import java.rmi.*;
public interface ComputeServer extends Remote {
Object compute(Task task) throws RemoteException;
}
The
Task
interface is generic, allowing a
ComputeServer
to do any compu-
tation requested of it:
public interface Task extends java.io.Serializable {
Object run();
}
Task
itself is not a remote interface. Each
ComputeServer
object will run on
a host and will be asked to execute tasks locally on its own host and re-