Skip to content

Instantly share code, notes, and snippets.

@patrickseda
Created September 7, 2011 15:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save patrickseda/1200900 to your computer and use it in GitHub Desktop.
Save patrickseda/1200900 to your computer and use it in GitHub Desktop.
Java ShutdownHook registration class for managing controlled shut downs.
/**
* Sample implementation of a Shutdownable object.
*/
public class SampleImpl implements Shutdownable {
public static void main(String[] args) {
SampleImpl sample = new SampleImpl();
// Make the JVM call our shutdown() method upon exit.
ShutdownHook.registerForShutdownHook(sample);
}
@Override
public void shutdown() {
// Perform resource cleanup of your Shutdownable implementation ...
}
}
/**
* Specifies the ability to perform a controlled shut down.
*/
public interface Shutdownable {
/** Perform controlled shut down. */
void shutdown();
}
/**
* This class provides the ability for an object to be registered with a JVM
* shutdown hook. The target Shutdownable object will have its shutdown() method
* called when the JVM exits.
*/
public final class ShutdownHook<T extends Shutdownable> implements Runnable {
private T target = null;
/** Constructor. */
private ShutdownHook(T target) {
this.target = target;
}
/** Run the shutdown hook for the target Shutdownable object. */
public void run() {
if (null != target) {
log.info("The " + target.getClass().getSimpleName() + " is being shut down ...");
target.shutdown();
}
}
/**
* Register an object to get notified during a JVM shutdown.
* <p>
* The type of the target must be an instantiation of the Shutdownable
* interface. When the ShutdownHook is called by the JVM, the shutdown()
* method of the target will be called.
* <p>
* The shutdown() method of the target should be judiciously coded, i.e. it
* should be thread-safe, finish its work quickly, and should not rely upon
* services that may have registered their own shutdown hooks.
* <p>
* It is possible that adding a shutdown hook may fail due to an Exception.
* These failures will not cause an exception to be thrown from this method.
*
* @param target The object to register.
*/
public static <T extends Shutdownable> void registerForShutdownHook(final T target) {
if (null != target) {
final ShutdownHook<T> shutdownHook = new ShutdownHook<T>(target);
try {
Runtime.getRuntime().addShutdownHook(new Thread(shutdownHook));
} catch (Exception ex) {
log.error("Could not add shutdown hook for target ["
+ target.getClass().getSimpleName() + "]. Exception = " + ex);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment