| CONTENTS | PREV | NEXT | Java Remote Method Invocation |
UnicastRemoteObject Class
The classjava.rmi.server.UnicastRemoteObjectprovides support for creating and exporting remote objects. The class implements a remote server object with the following characteristics:
package java.rmi.server;
public class UnicastRemoteObject extends RemoteServer {
protected UnicastRemoteObject()
throws java.rmi.RemoteException {...}
protected UnicastRemoteObject(int port)
throws java.rmi.RemoteException {...}
protected UnicastRemoteObject(int port,
RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
throws java.rmi.RemoteException {...}
public Object clone()
throws java.lang.CloneNotSupportedException {...}
public static RemoteStub exportObject(java.rmi.Remote obj)
throws java.rmi.RemoteException {...}
public static Remote exportObject(java.rmi.Remote obj, int port)
throws java.rmi.RemoteException {...}
public static Remote exportObject(Remote obj, int port,
RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
throws java.rmi.RemoteException {...}
public static boolean unexportObject(java.rmi.Remote obj,
boolean force)
throws java.rmi.NoSuchObjectException {...}
}
A remote object implementation (one that implements one or more remote interfaces) must be created and exported. Exporting a remote object makes that object available to accept incoming calls from clients. For a remote object implementation that is exported as aUnicastRemoteObject, the exporting involves listening on a TCP port (note that more than one remote object can accept incoming calls on the same port, so listening on a new port is not always necessary). A remote object implementation can extend the classUnicastRemoteObjectto make use of its constructors that export the object, or it can extend some other class (or none at all) and export the object viaUnicastRemoteObject'sexportObjectmethods.The no argument constructor creates and exports a remote object on an anonymous (or arbitrary) port, chosen at runtime. The second form of the constructor takes a single argument, port, that specifies the port number on which the remote object accepts incoming calls. The third constructor creates and exports a remote object that accepts incoming calls on the specified port via a
ServerSocketcreated from theRMIServerSocketFactory; clients will make connections to the remote object viaSockets supplied from theRMIClientSocketFactory.
RemoteObject
AnexportObjectmethod (any of the forms) is used to export a simple peer-to-peer remote object that is not implemented by extending theUnicastRemoteObjectclass. The first form of theexportObjectmethod takes a single parameter, obj, which is the remote object that will accept incoming RMI calls; thisexportObjectmethod exports the object on an anonymous (or arbitrary) port, chosen at runtime. The secondexportObjectmethod takes two parameters, both the remote object, obj, and port, the port number on which the remote object accepts incoming calls. The thirdexportObjectmethod exports the object, obj, with the specifiedRMIClientSocketFactory, csf, andRMIServerSocketFactory, ssf, on the specified port.The
exportObjectmethod returns aRemotestub which is the stub object for the remote object,obj, that is passed in place of the remote object in an RMI call.
UnicastRemoteObject in an RMI Call
As stated above, when an exported object of typeUnicastRemoteObjectis passed as a parameter or return value in an RMI call, the object is replaced by the remote object's stub. An exported remote object implementation remains in the virtual machine in which it was created and does not move (even by value) from that virtual machine. In other words, an exported remote object is passed by reference in an RMI call; exported remote object implementations cannot be passed by value.
UnicastRemoteObject
Information contained inUnicastRemoteObjectis transient and is not saved if an object of that type is written to a user-definedObjectOutputStream(for example, if the object is written to a file using serialization). An object that is an instance of a user-defined subclass ofUnicastRemoteObject, however, may have non-transient data that can be saved when the object is serialized.When a
UnicastRemoteObjectis read from anObjectInputStreamusingUnicastRemoteObject'sreadObjectmethod, the remote object is automatically exported to the RMI runtime so that it may receive RMI calls. If exporting the object fails for some reason, deserializing the object will terminate with an exception.
UnicastRemoteObject
TheunexportObjectmethod makes the remote object, obj, unavailable for incoming calls. If the force parameter is true, the object is forcibly unexported even if there are pending calls to the remote object or the remote object still has calls in progress. If the force parameter is false, the object is only unexported if there are no pending or in-progress calls to the object. If the object is successfully unexported, the RMI runtime removes the object from its internal tables. Unexporting the object in this forcible manner may leave clients holding stale remote references to the remote object. This method throwsjava.rmi.NoSuchObjectExceptionif the object was not previously exported to the RMI runtime.
clone method
Objects are only clonable using the Java programming language's default mechanism if they support thejava.lang.Cloneableinterface. The classjava.rmi.server.UnicastRemoteObjectdoes not implement this interface, but does implement theclonemethod so that if subclasses need to implementCloneable, the remote object will be capable of being cloned properly. Theclonemethod can be used by a subclass to create a cloned remote object with initially the same contents, but is exported to accept remote calls and is distinct from the original object.