2

I would like to stop the RMI registry programmatically. I thought this would be as trivial as calling some stop() or shutdown() method on classes LocateRegistry or Naming but there is no such method.

Things I have tried

  • I have tried to unregister all services and wait for the Registry's thread to end by itself gracefully, as proposed in some sites, but it does not seem to work (see #2 below).
  • I have tried the recipe provided in this similar question but although it stops the Registry the program as a whole does not stop (see #3 below).
  • I have taken into account the answer provided in this other question and made sure that I unexport the actual result of creating the registry and not a stub (see #3 below).

Code examples

  1. If I have a trivial program that only creates the registry and does nothing else, it ends successfully.

    // This is the only code in main() Registry registry = LocateRegistry.createRegistry(1099); // reaches the end of main() and stops

  2. As soon as a service is registered, the Java Virtual Machine will not stop --- presumably because the Registry's has a thread running in the background. This would make sense if there were services registered but not so much if there are none (at least given the behaviour described above in #1):

    // This is the only code in main() Registry registry = LocateRegistry.createRegistry(1099); Naming.rebind("///name", new SomeRemoteServer()); Naming.unbind("name"); // never stops - must be killed with Ctrl-C

  3. At this point, it does not make any different if I try to unexport the Registry. The Registry is stopped (no more services can be registered to / unregistered from it) but the program continues running.

    // This is the only code in main() Registry registry = LocateRegistry.createRegistry(1099); Naming.rebind("///name", new EchoServer()); Naming.unbind("name"); if (registry != null) { UnicastRemoteObject.unexportObject(registry, true); } // // If executed, next line throws NoSuchObjectException // Naming.rebind("///name", new EchoServer()); // // main program never stops - must be killed with Ctrl-C

In short, the question is whether it is possible to stop the RMI Registry programmatically once (a) it has been created and (b) a service has been registered on it? Thanks in advance.

Community
  • 1
  • 1
sergut
  • 289
  • 2
  • 8
  • Thanks for your comment. I have edited this question to make it explicit that it is not addressed by the solution to that other question. – sergut Dec 26 '16 at 19:36
  • You need to unexport all remote objects as well as the Registry. You don't need to unbind. There is a Java dot release somewhere in 1.5 that didn't stop the listening thread but that was many years ago. Duplicate. – user207421 Dec 26 '16 at 21:08
  • "You need to unexport all remote objects as well as the Registry". Thank you very much, that was the missing piece. I think your comment deserves to be a answer as IMHO it was not very explicit in the documentation I have read or similar questions in SE. If you write that as an answer for future reference, I will select it. :-) – sergut Dec 27 '16 at 15:10
  • You're really asking a different question here. You *have* stopped the Registry. You haven't stopped the *JVM*, and that's because objects are still exported from it. – user207421 Jan 01 '17 at 21:52

0 Answers0