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

[1714 byte] By [MarkusPa] at [2007-11-26 15:43:46]
# 1
First, the object needs to implement Remote. Does it? And why does it implement Serializable?
_dnoyeBa at 2007-7-8 22:02:46 > top of Java-index,Core,Core APIs...
# 2

See http://java.sun.com/developer/onlineTraining/rmi/exercises/DistributedGarbageCollector/index.html.

The interface MessageObject implements Remote. The implementation class does not need to implement Serializable, but it is not wrong, because *every* UnicastRemoteObject has to be serializable. UnicastRemoteObject itself implements Serializable...

I bypassed the problem by returning the instance "mo" of MessgeObjectImpl instead of the result of exportObject. Now it does work.

Moreover if I would not have extended Remote, there would not be a NoSuchObjectException but an UnmarshallingException.

MarkusPa at 2007-7-8 22:02:46 > top of Java-index,Core,Core APIs...