From 86aed458dd44213e47bad9ea8afd4e5cbc2d30a8 Mon Sep 17 00:00:00 2001 From: rob bygrave Date: Fri, 9 Oct 2020 14:13:52 +1300 Subject: [PATCH] Move ShutdownManager into io.ebean.event --- .../src/main/java/io/ebean/Database.java | 5 +++ .../api => ebean/event}/ClassUtil.java | 10 ++--- .../ebean/event/ServletContextListener.java | 2 - .../lib => ebean/event}/ShutdownManager.java | 44 +++++++++++-------- .../io/ebeaninternal/api/SpiEbeanServer.java | 5 --- .../server/core/DefaultContainer.java | 2 +- .../server/core/DefaultServer.java | 11 ++--- .../server/lib/ShutdownHook.java | 22 ---------- .../ebeaninternal/api/TDSpiEbeanServer.java | 2 +- .../server/lib/ShutdownManagerTest.java | 9 ++-- 10 files changed, 45 insertions(+), 67 deletions(-) rename ebean-core/src/main/java/io/{ebeaninternal/api => ebean/event}/ClassUtil.java (86%) rename ebean-core/src/main/java/io/{ebeaninternal/server/lib => ebean/event}/ShutdownManager.java (84%) delete mode 100644 ebean-core/src/main/java/io/ebeaninternal/server/lib/ShutdownHook.java diff --git a/ebean-core/src/main/java/io/ebean/Database.java b/ebean-core/src/main/java/io/ebean/Database.java index d918f3292..ea08efaa5 100644 --- a/ebean-core/src/main/java/io/ebean/Database.java +++ b/ebean-core/src/main/java/io/ebean/Database.java @@ -93,6 +93,11 @@ import java.util.concurrent.Callable; */ public interface Database { + /** + * Shutdown the Database instance. + */ + void shutdown(); + /** * Shutdown the Database instance programmatically. *

diff --git a/ebean-core/src/main/java/io/ebeaninternal/api/ClassUtil.java b/ebean-core/src/main/java/io/ebean/event/ClassUtil.java similarity index 86% rename from ebean-core/src/main/java/io/ebeaninternal/api/ClassUtil.java rename to ebean-core/src/main/java/io/ebean/event/ClassUtil.java index cafd39c16..6f8173d21 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/api/ClassUtil.java +++ b/ebean-core/src/main/java/io/ebean/event/ClassUtil.java @@ -1,15 +1,15 @@ -package io.ebeaninternal.api; +package io.ebean.event; /** * Helper to find classes taking into account the context class loader. */ -public class ClassUtil { +class ClassUtil { /** * Return a new instance of the class using the default constructor. */ - public static Object newInstance(String className) { + static Object newInstance(String className) { try { Class cls = forName(className); @@ -23,7 +23,7 @@ public class ClassUtil { /** * Load a class taking into account a context class loader (if present). */ - public static Class forName(String name) throws ClassNotFoundException { + static Class forName(String name) throws ClassNotFoundException { return new ClassLoadContext().forName(name); } @@ -31,7 +31,7 @@ public class ClassUtil { /** * Helper to wrap the context and caller classLoaders (to use/try both). */ - static class ClassLoadContext { + private static class ClassLoadContext { private final ClassLoader contextLoader; diff --git a/ebean-core/src/main/java/io/ebean/event/ServletContextListener.java b/ebean-core/src/main/java/io/ebean/event/ServletContextListener.java index d0b8b2222..7f89fccaa 100644 --- a/ebean-core/src/main/java/io/ebean/event/ServletContextListener.java +++ b/ebean-core/src/main/java/io/ebean/event/ServletContextListener.java @@ -1,7 +1,5 @@ package io.ebean.event; -import io.ebeaninternal.server.lib.ShutdownManager; - import javax.servlet.ServletContextEvent; /** diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/lib/ShutdownManager.java b/ebean-core/src/main/java/io/ebean/event/ShutdownManager.java similarity index 84% rename from ebean-core/src/main/java/io/ebeaninternal/server/lib/ShutdownManager.java rename to ebean-core/src/main/java/io/ebean/event/ShutdownManager.java index beed51f59..32ef7b846 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/lib/ShutdownManager.java +++ b/ebean-core/src/main/java/io/ebean/event/ShutdownManager.java @@ -1,8 +1,7 @@ -package io.ebeaninternal.server.lib; +package io.ebean.event; +import io.ebean.Database; import io.ebean.service.SpiContainer; -import io.ebeaninternal.api.ClassUtil; -import io.ebeaninternal.api.SpiEbeanServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,13 +22,13 @@ public final class ShutdownManager { private static final Logger logger = LoggerFactory.getLogger(ShutdownManager.class); - static final List servers = new ArrayList<>(); + private static final List databases = new ArrayList<>(); - static final ShutdownHook shutdownHook = new ShutdownHook(); + private static final ShutdownHook shutdownHook = new ShutdownHook(); - static boolean stopping; + private static boolean stopping; - static SpiContainer container; + private static SpiContainer container; static { // Register the Shutdown hook @@ -58,7 +57,7 @@ public final class ShutdownManager { */ public static boolean isStopping() { //noinspection SynchronizationOnStaticField - synchronized (servers) { + synchronized (databases) { return stopping; } } @@ -76,7 +75,7 @@ public final class ShutdownManager { */ public static void deregisterShutdownHook() { //noinspection SynchronizationOnStaticField - synchronized (servers) { + synchronized (databases) { try { Runtime.getRuntime().removeShutdownHook(shutdownHook); } catch (IllegalStateException ex) { @@ -92,7 +91,7 @@ public final class ShutdownManager { */ protected static void registerShutdownHook() { //noinspection SynchronizationOnStaticField - synchronized (servers) { + synchronized (databases) { try { String value = System.getProperty("ebean.registerShutdownHook"); if (value == null || !value.trim().equalsIgnoreCase("false")) { @@ -114,7 +113,7 @@ public final class ShutdownManager { */ public static void shutdown() { //noinspection SynchronizationOnStaticField - synchronized (servers) { + synchronized (databases) { if (stopping) { // Already run shutdown... return; @@ -146,9 +145,9 @@ public final class ShutdownManager { // shutdown any registered servers that have not // already been shutdown manually - for (SpiEbeanServer server : servers) { + for (Database server : databases) { try { - server.shutdownManaged(); + server.shutdown(); } catch (Exception ex) { logger.error("Error executing shutdown runnable", ex); ex.printStackTrace(); @@ -178,10 +177,10 @@ public final class ShutdownManager { /** * Register an ebeanServer to be shutdown when the JVM is shutdown. */ - public static void registerEbeanServer(SpiEbeanServer server) { + public static void registerDatabase(Database server) { //noinspection SynchronizationOnStaticField - synchronized (servers) { - servers.add(server); + synchronized (databases) { + databases.add(server); } } @@ -191,10 +190,17 @@ public final class ShutdownManager { * This is done when the ebeanServer is shutdown manually. *

*/ - public static void unregisterEbeanServer(SpiEbeanServer server) { + public static void unregisterDatabase(Database server) { //noinspection SynchronizationOnStaticField - synchronized (servers) { - servers.remove(server); + synchronized (databases) { + databases.remove(server); + } + } + + private static class ShutdownHook extends Thread { + @Override + public void run() { + ShutdownManager.shutdown(); } } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/api/SpiEbeanServer.java b/ebean-core/src/main/java/io/ebeaninternal/api/SpiEbeanServer.java index caa195b10..14d0cc148 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/api/SpiEbeanServer.java +++ b/ebean-core/src/main/java/io/ebeaninternal/api/SpiEbeanServer.java @@ -47,11 +47,6 @@ public interface SpiEbeanServer extends ExtendedServer, EbeanServer, BeanCollect */ SpiJsonContext jsonExtended(); - /** - * For internal use, shutdown of the server invoked by JVM Shutdown. - */ - void shutdownManaged(); - /** * Return true if updates in JDBC batch should include all columns if unspecified on the transaction. */ diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java b/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java index 0680ecb82..74b3eddc2 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultContainer.java @@ -17,7 +17,7 @@ import io.ebeaninternal.dbmigration.DbOffline; import io.ebeaninternal.server.cluster.ClusterManager; import io.ebeaninternal.server.core.bootup.BootupClassPathSearch; import io.ebeaninternal.server.core.bootup.BootupClasses; -import io.ebeaninternal.server.lib.ShutdownManager; +import io.ebean.event.ShutdownManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java b/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java index 698cea307..a14c41b3c 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java @@ -96,7 +96,7 @@ import io.ebeaninternal.server.dto.DtoBeanDescriptor; import io.ebeaninternal.server.dto.DtoBeanManager; import io.ebeaninternal.server.el.ElFilter; import io.ebeaninternal.server.grammer.EqlParser; -import io.ebeaninternal.server.lib.ShutdownManager; +import io.ebean.event.ShutdownManager; import io.ebeaninternal.server.query.CQuery; import io.ebeaninternal.server.query.CQueryEngine; import io.ebeaninternal.server.query.CallableQueryCount; @@ -300,7 +300,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { configureServerPlugins(); // Register with the JVM Shutdown hook - ShutdownManager.registerEbeanServer(this); + ShutdownManager.registerDatabase(this); } /** @@ -456,11 +456,8 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { } } - /** - * Shutting down via JVM Shutdown hook. - */ @Override - public void shutdownManaged() { + public void shutdown() { synchronized (this) { shutdownInternal(true, false); } @@ -473,7 +470,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { public void shutdown(boolean shutdownDataSource, boolean deregisterDriver) { synchronized (this) { // Unregister from JVM Shutdown hook - ShutdownManager.unregisterEbeanServer(this); + ShutdownManager.unregisterDatabase(this); shutdownInternal(shutdownDataSource, deregisterDriver); } } diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/lib/ShutdownHook.java b/ebean-core/src/main/java/io/ebeaninternal/server/lib/ShutdownHook.java deleted file mode 100644 index dd617e381..000000000 --- a/ebean-core/src/main/java/io/ebeaninternal/server/lib/ShutdownHook.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.ebeaninternal.server.lib; - - -/** - * This is the ShutdownHook that gets added to Runtime. - * It will try to shutdown the system cleanly when the JVM exits. - * It is best to add your own shutdown hooks to StartStop. - */ -class ShutdownHook extends Thread { - - ShutdownHook() { - } - - /** - * Fired by the JVM Runtime on shutdown. - */ - @Override - public void run() { - ShutdownManager.shutdown(); - } - -} diff --git a/ebean-core/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java b/ebean-core/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java index 2d30bb15b..0416dc5b8 100644 --- a/ebean-core/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java +++ b/ebean-core/src/test/java/io/ebeaninternal/api/TDSpiEbeanServer.java @@ -107,7 +107,7 @@ public class TDSpiEbeanServer implements SpiEbeanServer { } @Override - public void shutdownManaged() { + public void shutdown() { } @Override diff --git a/ebean-core/src/test/java/io/ebeaninternal/server/lib/ShutdownManagerTest.java b/ebean-core/src/test/java/io/ebeaninternal/server/lib/ShutdownManagerTest.java index 429e660cd..e5f8c3853 100644 --- a/ebean-core/src/test/java/io/ebeaninternal/server/lib/ShutdownManagerTest.java +++ b/ebean-core/src/test/java/io/ebeaninternal/server/lib/ShutdownManagerTest.java @@ -1,7 +1,8 @@ package io.ebeaninternal.server.lib; import io.ebean.BaseTestCase; -import io.ebean.Ebean; +import io.ebean.DB; +import io.ebean.event.ShutdownManager; import org.junit.Ignore; import org.junit.Test; @@ -13,8 +14,7 @@ public class ShutdownManagerTest extends BaseTestCase { @Ignore @Test public void test_deregisterShutdownHook() { - - Ebean.getDefaultServer(); + DB.getDefault(); ShutdownManager.deregisterShutdownHook(); } @@ -24,9 +24,8 @@ public class ShutdownManagerTest extends BaseTestCase { @Ignore @Test public void test_noShutdownHook() { - System.setProperty("ebean.registerShutdownHook", "false"); - Ebean.getDefaultServer(); + DB.getDefault(); } }