JimJim2000 JimJim2000 - 4 months ago 44
Java Question

RMI: RMI Reaper thread, UnicastRemoteObject and Registry

I have problem with RMI Reaper thread which doesn't allow all my program to terminate as this thread is not daemon. I found information that this thread stops only when all objects are unexported. So I have the following code to create rmi serer.

registry = LocateRegistry.createRegistry(2525);
Server stub = (Server) UnicastRemoteObject.exportObject(defaultServer, 2525);
registry.rebind("//localhost/server", stub);


And I have two versions of "unexporting".

Version 1:

UnicastRemoteObject.unexportObject(defaultServer, true);


Version 2:

UnicastRemoteObject.unexportObject(registry, true);


When I use version 2 - RMI Reaper thread doesn't terminate and all the program doesn't terminate. When I use version 1 everything ok. However, in many places I found the following code:

UnicastRemoteObject.unexportObject(rmiRegistry, true); // Stop the RMI registry


Could anyone explain why and how Registry is linked with UnicastRemoteObject.

EJP EJP
Answer
registry = LocateRegistry.createRegistry(2525);

That exports a Registry.

Server stub = (Server) UnicastRemoteObject.exportObject(defaultServer, 2525);

That exports your remote object defaultServer.

Ergo, when unexporting, you have to use both the lines you mentioned:

UnicastRemoteObject.unexportObject(defaultServer, true);

unexports your own remote object, and

UnicastRemoteObject.unexportObject(registry, true);

unexports the Registry that you started.

how Registry is linked with UnicastRemoteObject.

You bound your remote object into the Registry. For DGC purposes that makes the Registry a client of your remote object, which keeps it alive.

Comments