Skip to content

Instantly share code, notes, and snippets.

@jroper
Created November 8, 2012 01:28
Show Gist options
  • Save jroper/4035857 to your computer and use it in GitHub Desktop.
Save jroper/4035857 to your computer and use it in GitHub Desktop.
Ebean shutdown hack, for shutting down an individual ebeanserver before the Ebean shutdown hook runs
package com.avaje.ebeaninternal.server.lib;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Exists to work around this issue:
*
* https://github.com/rbygrave/avaje-ebeanorm-server/issues/4
*
* As luck would have it, Ebean tends to use package private fields, which allows us to do hacks like this without
* using reflection.
*
* @author jroper
*/
public class EbeanShutdownHack {
private static final Logger logger = Logger.getLogger(EbeanShutdownHack.class.getName());
public static void shutdownAllActiveEbeanServers() {
// First remove all the background thread runnables. To do this, we need to synchronize on its monitor field,
// which is private
Object monitor = getPrivateField(BackgroundThread.class, "monitor",
getPrivateField(BackgroundThread.class, "me", null));
synchronized (monitor) {
Iterator<BackgroundRunnable> runnables = BackgroundThread.runnables();
while (runnables.hasNext()) {
runnables.next();
runnables.remove();
}
}
// Now shut down the ebean servers
synchronized (ShutdownManager.runnables) {
for (Runnable runnable: ShutdownManager.runnables) {
try {
runnable.run();
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}
ShutdownManager.runnables.clear();
}
}
private static Object getPrivateField(Class clazz, String name, Object object) {
try {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
return field.get(object);
} catch (Exception e) {
throw new RuntimeException("Error getting private filed " + name + " on class " + clazz + " from object " + object, e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment