1

According to the Apache Commons Daemon project:

In case of a system-wide shutdown, the Virtual Machine process may be shut down directly by the operating system without notifying the running server application.

So I'm wondering: what is the value commons-daemon adds when you implement it? If I have an Oracle GlassFish Server instance running, and something happens (OOME, system-wide meltdown, etc.) that would normally send a SIGTERM or a SIGKILL to the JVM running OGS and all of its deployed apps, how could commons-daemon intervene and allows OGS and its deployed apps to shutdown quietly/politely?

And, if that's not what commons-daemon is for, can someone please explain to me a use case where it is used and helpful? Thanks in advance.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

1 Answers1

1

According to the docs, operating systems have support for a special class of server/daemon programs, and when the OS is about to shut down, it will send those a signal (before the actual SIGTERM/SIGKILL I guess) to notify them about it. Commons Daemon can interface with that.

I am not sure if this is any help if someone terminates the process directly, but if you use the proper service management tools of the OS, then the app probably has enough time to clean up.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Thanks @Thilo - I guess that makes sense (+1). So if I wanted to use Commons Daemon with a web or server container (Tomcat/GlassFish/etc.), how would I configure it (and/or the container) so that if the OS tries to shutdown the JVM that the container is running in, I have a chance to execute some shutdown hook of my own? Where would this shutdown hook live? – IAmYourFaja Jun 21 '12 at 23:16
  • If you are running on the J2EE stack, I would assume that all the usual life-cycle hooks (onContextDestroyed(), destroy() etc) get called by the container, so you'd put your code there. – Thilo Jun 21 '12 at 23:36
  • Sorry for being ambiguous there - what I meant to ask was: it seems like with `commons-daemon` there is perhaps an API your app need to implement, or at the very least you need to configure `commons-daemon` to register/hook your app. But what would this look like when the "app" you wish to make a daemon/service is in fact a container like Tomcat or OGS? In that case the JVM is not a Java process for your app, its a process for the *container* your app is deployed to. Essentially: **Is it possible to get `commons-daemon` to treat Tomcat/OGS like a daemon/service?** – IAmYourFaja Jun 21 '12 at 23:43
  • Again, thanks - but still confused. If I run Tomcat as a daemon (per the instructions in the link above), do I write the code that executes before Tomcat gets shutdown (at the "Tomcat-level" thus I can interact with Tomcat's actual code/classes/etc.), or does this just give me a chance to do stuff in `contextDestroyed` (implying that I can't write the Tomcat-level shutdown hook, but *can* write the app/deploy-level shutdown hooks)? – IAmYourFaja Jun 22 '12 at 00:04
  • That is my understanding, yes. The daemon support should make sure that the lifecycle hooks defined by the servlet and other API are executed properly. As an application programmer, you do not interact directly with the container implementation. – Thilo Jun 22 '12 at 00:09