From 6e453f616932f69b8c720d4e948380204e046d29 Mon Sep 17 00:00:00 2001 From: Gelin Luo Date: Sat, 2 Jan 2021 11:19:57 +1100 Subject: [PATCH] Shutdown app gracefully in Runtime shutdown hook --- CHANGELOG.md | 1 + VERSION_MATRIX.md | 42 +++++++++++++++++----------------- pom.xml | 2 +- src/main/java/act/Act.java | 38 ++++++++++++++++++++++++++---- src/main/java/act/app/App.java | 2 +- 5 files changed, 57 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1431a157..693a7326 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # ActFramework Change Log **1.9.1** +* Shutdown app gracefully in Runtime shutdown hook * Act-Test - support sending request with file array #1375 * It reports `UNKNOWN` for OS when running act on macOS #1373 * Hot reload not working for Bundle Resource properties #1372 diff --git a/VERSION_MATRIX.md b/VERSION_MATRIX.md index f7c5abaa..8b75fb2e 100644 --- a/VERSION_MATRIX.md +++ b/VERSION_MATRIX.md @@ -1,26 +1,26 @@ # Version Matrix -| act | 1.8.26 | 1.8.27 | 1.8.28 | 1.8.29 | 1.8.30a | 1.8.31 | 1.8.32 | 1.9.0a | -| --- | ----: | ----: | ----: | ----: | -----: | -----: | -----: | -----: | -| aaa | 1.6.0 | 1.6.1 | 1.6.1 | 1.7.0 | 1.7.0 | 1.7.3 | 1.8.0 | 1.10.0 | -| beetl | 1.6.0 | 1.6.1 | 1.6.1 | 1.7.0 | 1.7.0 | 1.7.1 | 1.7.2 | 1.8.0 | -| beetlsql | 1.7.0 | 1.7.1 | 1.7.1 | 1.8.0 | 1.8.0 | 1.8.1 | 1.8.2 | 1.8.2 | -| ebean-java7 | 1.7.8 | 1.7.8 | 1.7.9 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | -| ebean(java8) | 1.7.9 | 1.7.9 | 1.7.10 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | -| eclipselink(java8) | 1.6.0 | 1.6.0 | 1.6.1 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | -| excel-java7 | | | | 1.8.0 | 1.8.0 | 1.8.1 | 1.9.1 | end | -| excel | 1.7.0 | 1.7.1 | 1.7.2 | 1.8.0 | 1.8.0 | 1.8.1 | 1.9.1 | 1.9.2 | -| freemarker | 1.3.6 | 1.3.7 | 1.3.7 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.1 | -| hibernate | 1.6.0 | 1.6.0 | 1.6.1 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | -| jax-rs(java8) | 1.0.5 | 1.0.5 | 1.0.5 | 1.1.0 | 1.1.0 | 1.1.0 | 1.1.0 | 1.1.0 | -| jpa-common | 1.6.0 | 1.6.0 | 1.6.1 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | -| morphia | 1.7.2 | 1.7.2 | 1.7.3 | 1.8.0 | 1.8.1 | 1.8.2 | 1.9.0 | 1.10.0 | -| mustache(java8) | 1.4.6 | 1.4.6 | 1.4.6 | 1.5.0 | 1.5.0 | 1.5.0 | 1.5.0 | 1.5.0 | -| social | 0.12.6 | 0.12.6 | 0.12.6 | 0.13.0 | 0.13.0 | 0.13.0 | 0.13.0 | 0.14.0 | -| sql-common | 1.5.0 | 1.5.0 | 1.5.1 | 1.6.0 | 1.6.0 | 1.6.0 | 1.6.0 | 1.6.0 | -| storage(java8) | 0.14.0 | 0.14.0 | 0.14.0 | 0.15.0 | 0.15.0 | 0.15.0 | 0.16.0 | 0.16.0 | -| thymeleaf | 1.3.6 | 1.3.6 | 1.3.6 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.0 | -| velocity | 1.3.6 | 1.3.7 | 1.3.7 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.1 | 1.4.1 | +| act 1.8.28 | 1.8.29 | 1.8.30a | 1.8.31 | 1.8.32 | 1.9.0a | 1.9.1b | +| --- ----: | ----: | -----: | -----: | -----: | -----: | -----: | +| aaa 1.6.1 | 1.7.0 | 1.7.0 | 1.7.3 | 1.8.0 | 1.10.0 | 1.10.0 | +| beetl 1.6.1 | 1.7.0 | 1.7.0 | 1.7.1 | 1.7.2 | 1.8.0 | 1.8.0 | +| beetlsql 1.7.1 | 1.8.0 | 1.8.0 | 1.8.1 | 1.8.2 | 1.8.2 | 1.8.2 | +| ebean-java7 1.7.9 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | +| ebean(java8) 1.7.10 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | 1.8.0 | +| eclipselink(java8) 1.6.1 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | +| excel-java7 | 1.8.0 | 1.8.0 | 1.8.1 | 1.9.1 | end | end | +| excel 1.7.2 | 1.8.0 | 1.8.0 | 1.8.1 | 1.9.1 | 1.9.2 | 1.9.2 | +| freemarker 1.3.7 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.1 | 1.4.1 | +| hibernate 1.6.1 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | +| jax-rs(java8) 1.0.5 | 1.1.0 | 1.1.0 | 1.1.0 | 1.1.0 | 1.1.0 | 1.1.0 | +| jpa-common 1.6.1 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | 1.7.0 | +| morphia 1.7.3 | 1.8.0 | 1.8.1 | 1.8.2 | 1.9.0 | 1.10.0 | 1.10.0 | +| mustache(java8) 1.4.6 | 1.5.0 | 1.5.0 | 1.5.0 | 1.5.0 | 1.5.0 | 1.5.0 | +| social 0.12.6 | 0.13.0 | 0.13.0 | 0.13.0 | 0.13.0 | 0.14.0 | 0.14.0 | +| sql-common 1.5.1 | 1.6.0 | 1.6.0 | 1.6.0 | 1.6.0 | 1.6.0 | 1.6.0 | +| storage(java8) 0.14.0 | 0.15.0 | 0.15.0 | 0.15.0 | 0.16.0 | 0.16.0 | 0.16.0 | +| thymeleaf 1.3.6 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.0 | +| velocity 1.3.7 | 1.4.0 | 1.4.0 | 1.4.0 | 1.4.1 | 1.4.1 | 1.4.1 | ## Note diff --git a/pom.xml b/pom.xml index 9491e37c..4a5af930 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ org.actframework act jar - 1.9.2-SNAPSHOT + 1.9.1b-SNAPSHOT ACT Framework The ACT full stack MVC framework diff --git a/src/main/java/act/Act.java b/src/main/java/act/Act.java index 17dd024e..18375256 100644 --- a/src/main/java/act/Act.java +++ b/src/main/java/act/Act.java @@ -370,23 +370,44 @@ public final class Act { shutdown(app(), 0); } + public static void shutdownNow() { + shutdown(app(), 0, false); + } + public static void shutdown(final App app) { shutdown(app, 0); } public static void shutdown(final App app, final int exitCode) { + shutdown(app, exitCode, true); + } + + private static void shutdown(final App app, final int exitCode, boolean async) { if (null == appManager) { return; } - new Thread() { - @Override - public void run() { + if (async) { + new Thread() { + @Override + public void run() { + try { + if (!appManager.unload(app)) { + app.destroy(); + } + } finally { + shutdownAct(exitCode); + } + } + }.start(); + } else { + try { if (!appManager.unload(app)) { app.destroy(); } + } finally { shutdownAct(exitCode); } - }.start(); + } } public static RequestServerRestart requestRestart() { @@ -1205,7 +1226,12 @@ public final class Act { return app().config().httpPort(); } + private static boolean shutdownStarted = false; private static void shutdownAct(int exitCode) { + if (shutdownStarted) { + return; + } + shutdownStarted = true; clearPidFile(); shutdownNetworkLayer(); destroyApplicationManager(); @@ -1219,6 +1245,7 @@ public final class Act { destroyMetricPlugin(); unloadConfig(); destroyNetworkLayer(); + LOGGER.info("All components shutdown, bye!"); if (0 != exitCode) { System.exit(exitCode); } @@ -1260,6 +1287,7 @@ public final class Act { @Override public void run() { clearPidFile(); + Act.shutdownNow(); } }); } catch (Exception e) { @@ -1271,7 +1299,7 @@ public final class Act { String pidFile = pidFile(); try { File file = new File(pidFile); - if (!file.delete()) { + if (null != file && file.canRead() && !file.delete()) { file.deleteOnExit(); } } catch (Exception e) { diff --git a/src/main/java/act/app/App.java b/src/main/java/act/app/App.java index 856e6733..37f483ae 100644 --- a/src/main/java/act/app/App.java +++ b/src/main/java/act/app/App.java @@ -661,7 +661,7 @@ public class App extends LogSupportedDestroyableBase { if (null == daemonRegistry) { return; } - info("App shutting down ...."); + info("Shutting down app [%s]....", name()); if (Act.isDev()) { for (HotReloadListener listener : hotReloadListeners) { listener.preHotReload();