Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard Shutdown Hook

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?

like image 881
heaphach Avatar asked Jul 08 '15 09:07

heaphach


1 Answers

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);
like image 151
scanning Avatar answered Nov 06 '22 06:11

scanning