CONTENTS | PREV | NEXT | Java Remote Method Invocation |
The interfaces and classes that are responsible for specifying the remote behavior of the RMI system are defined in the java.rmi package hierarchy. The following figure shows the relationship between several of these interfaces and classes:
java.rmi.Remote
Interface
In RMI, a remote interface is an interface that declares a set of methods that may be invoked from a remote Java virtual machine. A remote interface must satisfy the following requirements:
- | A remote method declaration must include the exception java.rmi.RemoteException (or one of its superclasses such as java.io.IOException or java.lang.Exception ) in its throws clause, in addition to any application-specific exceptions (note that application specific exceptions do not have to extend java.rmi.RemoteException ). |
- | In a remote method declaration, a remote object declared as a parameter or return value (either declared directly in the parameter list or embedded within a non-remote object in a parameter) must be declared as the remote interface, not the implementation class of that interface. |
The interfacejava.rmi.Remote
is a marker interface that defines no methods:
public interface Remote {}
A remote interface must at least extend the interfacejava.rmi.Remote
(or another remote interface that extendsjava.rmi.Remote
). However, a remote interface may extend a non-remote interface under the following condition:
For example, the following interfaceBankAccount
defines a remote interface for accessing a bank account. It contains remote methods to deposit to the account, to get the account balance, and to withdraw from the account:
public interface BankAccount extends java.rmi.Remote { public void deposit(float amount) throws java.rmi.RemoteException; public void withdraw(float amount) throws OverdrawnException, java.rmi.RemoteException; public float getBalance() throws java.rmi.RemoteException; }
The next example shows a valid remote interfaceBeta
that extends a non-remote interfaceAlpha
, which has remote methods, and the interfacejava.rmi.Remote
:
public interface Alpha { public final String okay = "constants are okay too"; public Object foo(Object obj) throws java.rmi.RemoteException; public void bar() throws java.io.IOException; public int baz() throws java.lang.Exception; } public interface Beta extends Alpha, java.rmi.Remote { public void ping() throws java.rmi.RemoteException; }
RemoteException
Class
Thejava.rmi.RemoteException
class is the superclass of exceptions thrown by the RMI runtime during a remote method invocation. To ensure the robustness of applications using the RMI system, each remote method declared in a remote interface must specifyjava.rmi.RemoteException
(or one of its superclasses such asjava.io.IOException
orjava.lang.Exception
) in its throws clause.The exception
java.rmi.RemoteException
is thrown when a remote method invocation fails for some reason. Some reasons for remote method invocation failure include:
The classRemoteException
is a checked exception (one that must be handled by the caller of a remote method and is checked by the compiler), not aRuntimeException
.
RemoteObject
Class and its Subclasses
RMI server functions are provided byjava.rmi.server.RemoteObject
and its subclasses,java.rmi.server.RemoteServer
andjava.rmi.server.UnicastRemoteObject
andjava.rmi.activation.Activatable
.
- The methods needed to create remote objects and export them (make them available to remote clients) are provided by the classes
UnicastRemoteObject
andActivatable
. The subclass identifies the semantics of the remote reference, for example whether the server is a simple remote object or is an activatable remote object (one that executes when invoked).
- The
java.rmi.server.UnicastRemoteObject
class defines a singleton (unicast) remote object whose references are valid only while the server process is alive.