What's with the Unreferenced interface? It would seem that a server should be able to use the unreferenced() method of this interface to obtain notification if a client crashes or otherwise becomes disconnected from the server. But it does not seem to work that way? Why?

Avi Kak

The RMI Specification document says:

The java.rmi.server.Unreferenced interface allows a server object to receive notification that there are no clients holding references to it. .... When the "reference" set becomes empty, the Unreferenced.unreferenced() method is invoked...
The reference set in this quote refers to the set of client VM's, each holding a reference to the remote object.

Reading this, one could draw the conclusion that if all the clients got disconnected from a server that has implemented the Unreferenced interface, the server would be notified of such disconnection by automatic invocation of the Unreferenced.unreferenced() method of this interface. But in reality that does not happen. See, for example, the code in the "Feedback and Comments" section of jGuru RMI FAQ Entry 1002. On the other side of the coin, one can see easily from the jGuru Distributed Garbage Collection Exercise that when there are no client references to a remote object that has implemented the Unreferenced interface, the server does indeed receive notification via the automatic invocation of the Unreferenced.unreferenced() method. So why does the RMI system work differently in the two cases?

This apparent anomaly can be explained by the fact that there is a distinction between remote objects that are registered with the RMI registry and remote objects that are not registered. The registry acts like a client with regard to the server objects that are registered. A registry holds a lease on a registered server object in much the same manner as a regular client. So even if all the genuine clients were to get disconnected from a server, the method Unreferenced.unreferenced() will not be invoked on a server object as long as it stays registered.