Commit 6297abcc authored by 朱劲松​'s avatar 朱劲松​

平滑退出,增加处理失败时的非正常退出.

parent d2ad80a6
...@@ -23,24 +23,33 @@ public class DefaultSignalHandler implements SignalHandler { ...@@ -23,24 +23,33 @@ public class DefaultSignalHandler implements SignalHandler {
@Override @Override
public void handle(Signal signal) { public void handle(Signal signal) {
String applicationName = context.getApplicationName();
log.info("开始执行停止{}服务", applicationName);
GracefulShutdownProperties bean = context.getBean(GracefulShutdownProperties.class);
try { try {
context.getBean(Shutdown.class).shutdown(bean.getTimeout()); String applicationName = context.getApplicationName();
log.info("servlet container 停止接收请求"); log.info("开始执行停止{}服务", applicationName);
} catch (InterruptedException e) { GracefulShutdownProperties bean = context.getBean(GracefulShutdownProperties.class);
} try {
context.getBean(Shutdown.class).shutdown(bean.getTimeout());
Thread.getAllStackTraces().forEach((thread, stackTraceElements) -> { log.info("servlet container 停止接收请求");
if (!thread.isDaemon()) { } catch (InterruptedException e) {
//如果中断 daemon 线程, 会导致 PreDestroy 方法不执行,
//如果不中断 non-daemon 线程, 会导致无法 exit(0).
log.debug("中断正在进行的 non-daemon 线程: {} ; {}", thread.getId(), thread.getName());
thread.interrupt();
} }
});
log.info("{} 即将执行 @PreDestroy 方法", applicationName); Thread.getAllStackTraces().forEach((thread, stackTraceElements) -> {
System.exit(0); if (!thread.isDaemon()) {
//如果中断 daemon 线程, 会导致 PreDestroy 方法不执行,
//如果不中断 non-daemon 线程, 会导致无法 exit(0).
log.debug("中断正在进行的 non-daemon 线程: {} ; {}", thread.getId(), thread.getName());
thread.interrupt();
}
});
log.info("{} 即将执行 @PreDestroy 方法", applicationName);
System.exit(0);
} catch (Exception e) {
// 此处可能导致异常的里面包含了logback中类未能找到, 所以增加输出到控制台.
System.out.println(e.getMessage());
log.error(e.getMessage(), e);
} finally {
System.out.println("强制退出.");
System.exit(1);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment