To receive invocations, an instance of a remote object must first be exported. Exporting associates the object with a unique object identifier, which is registered by the RMI infrastructure and used by the stubs. A remote object can simply inherit from the UnicastRemoteObject
class, which exports the object in the inherited constructor. Alternatively, a remote object can be registered by calling the exportObject
method and unregistered by calling the unexportObject
method, both provided by the UnicastRemoteObject
class.
public class ExampleImpl extends UnicastRemoteObject implements Example { public ExampleImpl () throws RemoteException { } public void printMessage (String message) { System.out.println (message); } }
static Remote exportObject (Remote obj, int port) static Remote exportObject (Remote obj, int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf) static boolean unexportObject (Remote obj, boolean force)
Passing an object by reference is done by passing its proxy by value. An object is passed by reference only when it is exported, objects that are not exported are passed by value even when they implement the Remote
interface.