-
-
Save thangdc94/87d6a3e431bc85ee7f53e06271adbe81 to your computer and use it in GitHub Desktop.
Java ShutdownHook registration class for managing controlled shut downs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Specifies the ability to perform a controlled shut down. | |
*/ | |
public interface Shutdownable { | |
/** Perform controlled shut down. */ | |
void shutdown(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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