Dropwizard Shutdown Hook
Asked Answered
N

2

7

The problem is, that I stop Dropwizard application (via ctrl + c) and I have inserted a Shutdown Hook in main class to do some stuff before shutdown. But now ServerConnector for the application is closed before I can do what I want to do.

There is a polling service (polls one of my resources) and I need to tell them, that application will go down soon to prevent some problems. I need at least 15 seconds before ressource goes down.

Some idea how to solve this problem?

Nide answered 8/7, 2015 at 9:31 Comment(0)
A
9

You can use a lifecycle hook to manage certain resources.

public class ManagedObject implements Managed {

    private final Object obj;

    public ManagedObject(Object obj) {
        this.obj = obj;
    }

    @Override
    public void start() throws Exception {
        // Do something to start the object
    }

    @Override
    public void stop() throws Exception {
        // Do something to stop the object
    }
}

Then register on the environment

ManagedObject myManagedObject = new ManagedObject(obj);
environment.lifecycle().manage(myManagedObject);
Autocrat answered 20/12, 2016 at 12:59 Comment(0)
L
6

Add a Dropwizard Task that will change the state of a static field (or however you want to pass the data) which your polling resource will be using to respond.

public class ShutdownTask extends Task {
    private int timeoutSeconds;

    public ShutdownTask (int timeoutSeconds) {
        super("shutdown");
        this.timeoutSeconds = timeoutSeconds;
    }

      @Override
    public void execute(ImmutableMultimap<String, String> parameters, PrintWriter output) throws Exception {
        // you probably can take the timeout parameter from the request via 'parameters' instead of the constructor.
        PollingResource.shuttingDownIn = timeoutSeconds;
    }
}


environment.admin().addTask(new ShutdownTask(15));

Then write a bash script which will curl to task

curl -X POST http://dw.example.com:8081/tasks/shutdown

And:

  • This is probably not recommended (people don't like System.exit(0)) but you can add the following to execute method:

Thread.sleep(timeoutSeconds * 1000); System.exit(0)

  • Or do the waiting and kill the dropwizard app in the bash script.

kill -SIGINT <pid>

Liking answered 8/7, 2015 at 15:13 Comment(8)
Yeah thats now the way I do it, but server admin or linux service stop command will still stop application in the 'old way'. Service ressource will be closed instant and I have no idea how to fix it at the moment.Nide
Yeah, so the idea of triggering the task is to put the server in "closing" state so the resources will be still available. And then you wait for n seconds while the server is in this state and then stop the service.Liking
But if you ctrl+c, the ressource is still going down and I want to prevent exactly this case :-)Nide
That's why instead of doing ctrl+c, you'll run the bash script like ./shutdown.sh 15Liking
And what happens if application is stopped not in this way? Then I have a big problem and thats why I need another way :-)Nide
Oh, I see. I was just writing it's probably not possible but coincidentally found a similar question in dropwizard group. groups.google.com/forum/#!topic/dropwizard-user/UP1krCq--cA . Looks like Managed Objects could be useful, or there seems to be a jetty configuration called http.shutdownGracePeriod. I can't post these as answers as I haven't tried them before but do post it yourself if they fixes your problem.Liking
Managed Objects does not help or I doing it wrong. The problem is still, that the application connector does not allow new connections even if i use managed objects. It seems that the appl. connector stops first.Nide
I have the exact same need. Any chance you stumbled on an answer?Engineman

© 2022 - 2024 — McMap. All rights reserved.