From bfa86f241b32ea70bf1622ef5da1b9b4a8d96f71 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Thu, 1 Oct 2015 10:33:09 +1300 Subject: [PATCH] #427 - ENH: Add support for Removing ShutdownManager.shutdownHook ... manually control entire shutdown. --- .../server/lib/ShutdownManager.java | 11 +++++-- .../server/lib/ShutdownManagerTest.java | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/avaje/ebeaninternal/server/lib/ShutdownManagerTest.java diff --git a/src/main/java/com/avaje/ebeaninternal/server/lib/ShutdownManager.java b/src/main/java/com/avaje/ebeaninternal/server/lib/ShutdownManager.java index 70bd654ae..8100e6816 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/lib/ShutdownManager.java +++ b/src/main/java/com/avaje/ebeaninternal/server/lib/ShutdownManager.java @@ -64,12 +64,16 @@ public final class ShutdownManager { /** * Deregister the Shutdown hook. + *

+ * In calling this method it is expected that application code will invoke + * the shutdown() method. + *

*

* For running in a Servlet Container a redeploy will cause a shutdown, and * for that case we need to make sure the shutdown hook is deregistered. *

*/ - protected static void deregisterShutdownHook() { + public static void deregisterShutdownHook() { synchronized (servers) { try { Runtime.getRuntime().removeShutdownHook(shutdownHook); @@ -87,7 +91,10 @@ public final class ShutdownManager { protected static void registerShutdownHook() { synchronized (servers) { try { - Runtime.getRuntime().addShutdownHook(shutdownHook); + String value = System.getProperty("ebean.registerShutdownHook"); + if (value == null || !value.trim().equalsIgnoreCase("false")) { + Runtime.getRuntime().addShutdownHook(shutdownHook); + } } catch (IllegalStateException ex) { if (!ex.getMessage().equals("Shutdown in progress")) { throw ex; diff --git a/src/test/java/com/avaje/ebeaninternal/server/lib/ShutdownManagerTest.java b/src/test/java/com/avaje/ebeaninternal/server/lib/ShutdownManagerTest.java new file mode 100644 index 000000000..3d554a56b --- /dev/null +++ b/src/test/java/com/avaje/ebeaninternal/server/lib/ShutdownManagerTest.java @@ -0,0 +1,32 @@ +package com.avaje.ebeaninternal.server.lib; + +import com.avaje.ebean.BaseTestCase; +import com.avaje.ebean.Ebean; +import org.junit.Ignore; +import org.junit.Test; + +public class ShutdownManagerTest extends BaseTestCase { + + /** + * Run this test manually. + */ + @Ignore + @Test + public void test_deregisterShutdownHook() { + + Ebean.getDefaultServer(); + ShutdownManager.deregisterShutdownHook(); + } + + /** + * Run this test manually. + */ + @Ignore + @Test + public void test_noShutdownHook() { + + System.setProperty("ebean.registerShutdownHook","false"); + Ebean.getDefaultServer(); + } + +} \ No newline at end of file