Package org.astrogrid.samp
Class ShutdownManager
- java.lang.Object
-
- org.astrogrid.samp.ShutdownManager
-
public class ShutdownManager extends java.lang.Object
Handles ordered running of cleanup processes at JVM shutdown. This is a singleton class, usegetInstance()
.- Since:
- 12 Oct 2012
- Author:
- Sylvain Lafrasse, Mark Taylor
-
-
Field Summary
Fields Modifier and Type Field Description static int
CLIENT_SEQUENCE
Shutdown sequence value for client hooks.static int
HUB_SEQUENCE
Shutdown sequence value for hub hooks.static int
PREPARE_SEQUENCE
Shutdown sequence for preparatory hooks.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
forceLog(java.util.logging.Logger logger, java.util.logging.Level level, java.lang.String msg, java.lang.Throwable error)
Writes a log-like message directly to standard error if it has an appropriate level.static ShutdownManager
getInstance()
Returns sole instance of this class.void
registerHook(java.lang.Object key, int iseq, java.lang.Runnable runnable)
Register a runnable to be run on shutdown with a given key and sequence.void
unregisterHook(java.lang.Object key)
Unregisters a key earlier registered usingregisterHook(java.lang.Object, int, java.lang.Runnable)
.
-
-
-
Field Detail
-
PREPARE_SEQUENCE
public static final int PREPARE_SEQUENCE
Shutdown sequence for preparatory hooks.- See Also:
- Constant Field Values
-
CLIENT_SEQUENCE
public static final int CLIENT_SEQUENCE
Shutdown sequence value for client hooks.- See Also:
- Constant Field Values
-
HUB_SEQUENCE
public static final int HUB_SEQUENCE
Shutdown sequence value for hub hooks.- See Also:
- Constant Field Values
-
-
Method Detail
-
registerHook
public void registerHook(java.lang.Object key, int iseq, java.lang.Runnable runnable)
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
public void unregisterHook(java.lang.Object key)
Unregisters a key earlier registered usingregisterHook(java.lang.Object, int, java.lang.Runnable)
.- Parameters:
key
- registration key
-
getInstance
public static ShutdownManager getInstance()
Returns sole instance of this class.- Returns:
- instance
-
forceLog
public static void forceLog(java.util.logging.Logger logger, java.util.logging.Level level, java.lang.String msg, java.lang.Throwable error)
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
-
-