This forum has been archived. Please start a new discussion on GitHub.

Calling communicator.destroy() from a JVM shutdown hook

Would it make sense to call communicator.destroy() in a JVM shutdown hook to make sure that it will get called whatever happens? Or can it take unreasonably long time?

Also, is there a reason finally blocks are not used for destroying a communicator in Java/C# client examples in the Reference Manual?


  • benoit
    benoit Rennes, France

    Yes, you can use a shutdown hook to destroy the communicator. You can also use the Ice.Application helper class and it will take care of installing a hook to either destroy or shutdown the Ice communicator. See this link for more information on the Ice.Application class.

    The communicator destroy call can take time to complete. If for example your Ice application is a server and there are some invocations being dispatched, destroy will wait until all the dispatched invocations are done. If there are still opened Ice connections, destroy will also gracefully close these connections and wait for the connections to be closed by the peer (which might also take some time if the peer doesn't promptly close the connections).

    The examples in the manual which are destroying the communicator could use finally blocks, there are no specific reasons for not using them.

  • Ice.Application looks useful. Except I have a GUI client, so need to show exception message in the window instead of the console. Is there a way to use Ice.Application with my own exception handler?
  • mes
    mes California

    It's not possible to set your own exception handler in Ice.Application. In fact, we usually recommend against using Ice.Application in a GUI client. For example, GUI clients often cannot block their main thread, but invoking Application.main() will block the calling thread until its communicator is destroyed.

    You can certainly copy the portions of Ice.Application that you find useful into your own class. Also, if you haven't done so already, you should familiarize yourself with the "dispatcher" facility (see section 32.10.7 here). It makes life simpler when writing graphical Ice applications.