Skip to content

Commit

Permalink
Merge pull request adamfisk#179 from jekh/remove-shutdown-hook-on-stop
Browse files Browse the repository at this point in the history
Remove JVM shutdown hook on stop()
  • Loading branch information
adamfisk committed Feb 18, 2015
2 parents e3acc48 + 474484f commit 4c60fa8
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,17 @@ private static class ServerGroup {

private volatile boolean stopped = false;

/**
* JVM shutdown hook to stop this server group. Declared as a class-level variable to allow removing the shutdown hook when the
* server is stopped normally.
*/
private final Thread serverGroupShutdownHook = new Thread(new Runnable() {
@Override
public void run() {
stop();
}
});

private ServerGroup(String name) {
this.name = name;

Expand All @@ -497,11 +508,7 @@ public void uncaughtException(final Thread t, final Throwable e) {
}
});

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
stop();
}
}));
Runtime.getRuntime().addShutdownHook(serverGroupShutdownHook);
}

public synchronized void ensureProtocol(
Expand Down Expand Up @@ -588,6 +595,9 @@ synchronized private void stop() {
}
}

// remove the shutdown hook that was added when the server group was started, since it has now been stopped
Runtime.getRuntime().removeShutdownHook(serverGroupShutdownHook);

stopped = true;

LOG.info("Done shutting down proxy");
Expand Down

0 comments on commit 4c60fa8

Please sign in to comment.