Move ShutdownManager into io.ebean.event

This commit is contained in:
rob bygrave
2020-10-09 14:13:52 +13:00
parent fa4130ba8e
commit 86aed458dd
10 changed files with 45 additions and 67 deletions
@@ -93,6 +93,11 @@ import java.util.concurrent.Callable;
*/
public interface Database {
/**
* Shutdown the Database instance.
*/
void shutdown();
/**
* Shutdown the Database instance programmatically.
* <p>
@@ -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;
@@ -1,7 +1,5 @@
package io.ebean.event;
import io.ebeaninternal.server.lib.ShutdownManager;
import javax.servlet.ServletContextEvent;
/**
@@ -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<SpiEbeanServer> servers = new ArrayList<>();
private static final List<Database> 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.
* </p>
*/
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();
}
}
}
@@ -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.
*/
@@ -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;
@@ -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);
}
}
@@ -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();
}
}
@@ -107,7 +107,7 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
}
@Override
public void shutdownManaged() {
public void shutdown() {
}
@Override
@@ -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();
}
}