don't hang the main thread, keep a different, non daemon thread alive till we shutdown

This commit is contained in:
kimchy 2010-05-01 03:09:44 +03:00
parent ebded19dc1
commit 5d8d2cf4f9
1 changed files with 13 additions and 8 deletions

View File

@ -54,6 +54,8 @@ public class Bootstrap {
private Node node;
private static Thread keepAliveThread;
private void setup(boolean addShutdownHook, Tuple<Settings, Environment> tuple) throws Exception {
tuple = setupJmx(tuple);
@ -167,22 +169,25 @@ public class Bootstrap {
System.err.close();
}
// keep this thread alive (non daemon thread) until we shutdown
final CountDownLatch latch = new CountDownLatch(1);
// keep this thread alive (non daemon thread) until we shutdown
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override public void run() {
latch.countDown();
}
});
while (true) {
try {
latch.await();
} catch (InterruptedException e) {
// bail out
keepAliveThread = new Thread(new Runnable() {
@Override public void run() {
try {
latch.await();
} catch (InterruptedException e) {
// bail out
}
}
break;
}
}, "es[keepAlive]");
keepAliveThread.setDaemon(false);
keepAliveThread.start();
} catch (Throwable e) {
ESLogger logger = Loggers.getLogger(Bootstrap.class);
if (bootstrap.node != null) {