Reputation: 1492
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?
Upvotes: 7
Views: 7383
Reputation: 2858
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:
System.exit(0)
) but you can add the following to execute method:Thread.sleep(timeoutSeconds * 1000);
System.exit(0)
kill -SIGINT <pid>
Upvotes: 6
Reputation: 111
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);
Upvotes: 9