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?

heaphach
  • 1,492
  • 1
  • 20
  • 44

2 Answers2

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);
scanning
  • 111
  • 1
  • 1
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>

davnicwil
  • 28,487
  • 16
  • 107
  • 123
Natan
  • 2,816
  • 20
  • 37
  • 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. – heaphach Jul 09 '15 at 07:27
  • 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. – Natan Jul 09 '15 at 07:47
  • But if you ctrl+c, the ressource is still going down and I want to prevent exactly this case :-) – heaphach Jul 09 '15 at 08:39
  • That's why instead of doing `ctrl+c`, you'll run the bash script like `./shutdown.sh 15` – Natan Jul 09 '15 at 09:48
  • 2
    And what happens if application is stopped not in this way? Then I have a big problem and thats why I need another way :-) – heaphach Jul 09 '15 at 12:19
  • Oh, I see. I was just writing it's probably not possible but coincidentally found a similar question in dropwizard group. https://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. – Natan Jul 09 '15 at 14:41
  • 1
    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. – heaphach Jul 16 '15 at 09:42
  • I have the exact same need. Any chance you stumbled on an answer? – Evan Richards Sep 13 '18 at 01:39