Stub/Exported object/Exportee lifecycle
When I export an object it creates a stub. I am wondering what is the lifetime of this stub. or more specifically, what is the lifetime of an exported object.
It seems that if a client calls a method, and in that method I export an object, but I do not hold that object locally, and I do not pass that object back to the client, then the stub / remote object can go stale at anytime.
Consider this
void MyObject remoteCall(){
Remote serverObj =new ServerObject();
UnicastRemoteObject.export(serverObj);
return serverObj;
}
In the above piece of code it is resonable to assume the GC/DGC can not collect the 'exported' object. That is, the stub will live because a reference to it is held until the RMI subsystem does its own substitution and returns the stub to the client. But consider code below
void MyObject remoteCall(){
return getObj();
}
Remote getObj(){
Remote serverObj =new ServerObject();
UnicastRemoteObject.export(serverObj);
serverObj;
}
In the above case, after the call getObj() completes, there is no strong reference to the stub. Thus, I expect that technically the DGC can collect it immediately. And my remoteCall method can fail since the object is no longer exported.
IIRC exported stubs/proxies are held with weak references.
I am trying to devise a system whereby the client can ask for an object, and the server will check in a local cache to see if it is already available and exported. But the unreferenced behavior is causing a problem because I can not guarantee the object is exported at anytime because this method can run at anytime unless I am holding a strong reference to the stub I guess!?
Perhaps that is a question there. If I hold a strong reference to the stub on the server will this keep the remote object exported? If I hold a strong reference to the remote object exported on the server, will this keep that object exported?
At this point I am starting to consider caching my 'remote' objects and simply exporting the object each time a client requests it, or I will have to maks sure the object is exported within the method call of the client and that the stub has a strong reference for the duration of the client call!?
I used to be confident, but I did not write enough comments in my code and so I am confused again :D

