Package org.astrogrid.samp
Class ShutdownManager
java.lang.Object
org.astrogrid.samp.ShutdownManager
Handles ordered running of cleanup processes at JVM shutdown.
This is a singleton class, use
getInstance()
.- Since:
- 12 Oct 2012
- Author:
- Sylvain Lafrasse, Mark Taylor
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Shutdown sequence value for client hooks.static final int
Shutdown sequence value for hub hooks.static final int
Shutdown sequence for preparatory hooks. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Writes a log-like message directly to standard error if it has an appropriate level.static ShutdownManager
Returns sole instance of this class.void
registerHook
(Object key, int iseq, Runnable runnable) Register a runnable to be run on shutdown with a given key and sequence.void
unregisterHook
(Object key) Unregisters a key earlier registered usingregisterHook(java.lang.Object, int, java.lang.Runnable)
.
-
Field Details
-
PREPARE_SEQUENCE
public static final int PREPARE_SEQUENCEShutdown sequence for preparatory hooks.- See Also:
-
CLIENT_SEQUENCE
public static final int CLIENT_SEQUENCEShutdown sequence value for client hooks.- See Also:
-
HUB_SEQUENCE
public static final int HUB_SEQUENCEShutdown sequence value for hub hooks.- See Also:
-
-
Method Details
-
registerHook
Register a runnable to be run on shutdown with a given key and sequence. Items with a smaller value ofiseq
are run earlier at shutdown. Suitable sequence values are given byHUB_SEQUENCE
andCLIENT_SEQUENCE
. Thekey
is kept in a WeakHashMap, so if it is GC'd, the runnable will never execute.- Parameters:
key
- key which can be used to unregister the hook lateriseq
- value indicating position in shutdown sequencerunnable
- to be run on shutdown
-
unregisterHook
Unregisters a key earlier registered usingregisterHook(java.lang.Object, int, java.lang.Runnable)
.- Parameters:
key
- registration key
-
getInstance
Returns sole instance of this class.- Returns:
- instance
-
forceLog
Writes a log-like message directly to standard error if it has an appropriate level. This method is only intended for use during the shutdown process, when the logging system may be turned off so that normal logging calls may get ignored (this behaviour is not as far as I know documented, but seems reliable in for example Oracle JRE1.5). There may be some good reason for logging services to be withdrawn during shutdown, so it's not clear that using this method is a good idea at all even apart from bypassing the logging system; therefore use it sparingly.- Parameters:
logger
- loggerlevel
- level of message to logmsg
- text of logging messageerror
- associated throwable if any; may be null
-