jgreen81
jgreen81

Reputation: 797

How to shut down nicely when using in-process Java RMI registry

Case

I make use of an in-process* Java RMI registry in my server process.

I bind 1 object in the registry.

My client process connects and looks up the binding and remote objects are passed around.

Now I want the server process to shut down cleanly, i.e. no more deamon-threads should exist. Using RMI, I call "close" on an object in my server process and the question is what this "close" method must actually do.

Question

How do I make sure that no non-daemon threads are running in a process using RMI exported objects?

As far as I can read, I must unexport** all exported objects for the JVM to shut down? Since many objects have been passed over RMI, I would have to maintain this set of these objects and unexport** each one.

I have read that any exported object should unexport itself when garbage collectod (can't find a reference right now) but my experience tells me that it can take a long time for some objects (especially distrubuted?) to be garbage collected so this is not really an option.

* Created using LocateRegistry
** Unexport is performed with UnicastRemoteObject

Upvotes: 5

Views: 600

Answers (1)

user207421
user207421

Reputation: 310980

How do I make sure that no non-daemon threads are running in a process using RMI exported objects?

Unexport them all, including the Registry. Note that this requires saving the return value of createRegistry().

As far as I can read, I must unexport** all exported objects for the JVM to shut down? Since many objects have been passed over RMI, I would have to maintain this set of these objects and unexport** each one.

Correct, unless you use the Unreferenced facility.

I have read that any exported object should unexport itself when garbage collected (can't find a reference right now) but my experience tells me that it can take a long time for some objects (especially distributed?) to be garbage collected so this is not really an option.

You can control all the DGC timeouts. It really is an option, via the Unreferenced interface.

Upvotes: 0

Related Questions