NoSuchObjectException thrown when using exportObject
Hello,
while dealing with some jGuru-exercise (http://java.sun.com/developer/onlineTraining/rmi/exercises/DistributedGarbageCollector/index.html) I encountered some problems with UnicastRemoteObject.exportObject( obj, 0).
I modified the mentioned exercise so that the server-side remote implementation classes do not extend UnicastRemoteObject anymore. Instead, the remote reference stubs will now be created by calling UnicastRemoteObject.exportObject( obj, 0) and returned to the client.
The modified code:
class MessageObjectImpl
//extends UnicastRemoteObject
implements MessageObject, Serializable, Unreferenced
public MessageObject getMessageObject()throws RemoteException
{
MessageObject mo =new MessageObjectImpl();
//return mo;
return (MessageObject) UnicastRemoteObject.exportObject(mo, 0);
}
Sometimes it works, sometimes it does not. In the latter case the following exception is thrown:
java.rmi.NoSuchObjectException: no such object in table
It seems as if the Garbage Collector finalizes the object right before the client is able to call a method on the stub.
Can somebody help me out and tell, why it works when the remote implementation class extend UnicastRemoteObject and why it does not work when using UnicastRemoteObject.exportObject(mo, 0).
Thanks in advance!
MP

