Orchestrating graceful shutdown between halt, systemd and Commons Daemon

730 views Asked by At

Please note: Commons Daemon hasn't had a release since 2013. So, if there is a more modern alternative for that (that's more compatible with systemd), then I'm all ears and open to any ideas!


I'm trying to get my Java 8 app deployed and running as a true Linux service managed by systemd. I have read through the Commons Daemon docs and I believe the way to accomplish this is as follows:

  1. Write a systemd service unit file, say, a /lib/systemd/system/myapp.service file; this will allow me to manually control my app via systemctl start myapp, etc. and will also allow me to do things like: configure my app to start when Linux starts up, always attempt to restart if it crashes, etc.
  2. I also then need to implement the Daemon interface (from Commons Daemon), which will allow my app to "listen" to events sent to it from systemd and invoke the correct Java methods when certain things happen (the app is started, stopped, restarted, Linux is shutdown, etc.).

My best attempt at such a Daemon impl:

public class MyApp implements Daemon {
    private BillingService billingService;
    private PizzaDeliveryService pizzaDeliveryService;

    public MyApp() {
        super();

        // Do NOT initialize any services from inside here.
    }

    public static void main(String[] args) {
        MyApp app = new MyApp();
    }

    @Override
    public void init(DaemonContext context) throws DaemonInitException, Exception {
        // 1. I assume this is where I do all my initializing,
        // and that I don't init anything in my default ctor above?
        billingService = new StripBillingService();
        pizzaDeliveryService = new LittleCaesarsDeliveryService();
        // etc.
    }

    @Override
    public void start() throws Exception {
        // 2. This is where my service starts actually doing things,
        // firing up other threads, etc.
    }

    @Override
    public void stop() throws Exception {
        // 3. Will this respond to a Linux 'sudo shutdown -P now'
        // command?
    }

    @Override
    public void destroy() {
        // 4. Where I'm supposed to actually release resources
        // and terminate gracefully.
    }
}

Is my understanding of the Daemon interface correct? Is it correct to simply instantiate a MyApp instance inside main, and then Commons Daemon take care of invoking init(...) and start(...) for me? Am I correct in not initializing any MyApp fields or doing dependency injection from the default constructor, and instead doing all that from inside init(...)?

More importantly (my actual question): what Linux commands will trigger the stop(...) method?

I ask because I intend on shutting the server down either by issuing sudo shutdown -P now or by sudo halt -p and am wondering which (if any) command will prompt systemd to invoke the MyApp#stop(...) method. Any ideas?

0

There are 0 answers