Class ShutdownManager

java.lang.Object
org.astrogrid.samp.ShutdownManager

public class ShutdownManager extends Object
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 Details

    • PREPARE_SEQUENCE

      public static final int PREPARE_SEQUENCE
      Shutdown sequence for preparatory hooks.
      See Also:
    • CLIENT_SEQUENCE

      public static final int CLIENT_SEQUENCE
      Shutdown sequence value for client hooks.
      See Also:
    • HUB_SEQUENCE

      public static final int HUB_SEQUENCE
      Shutdown sequence value for hub hooks.
      See Also:
  • Method Details

    • registerHook

      public void registerHook(Object key, int iseq, Runnable runnable)
      Register a runnable to be run on shutdown with a given key and sequence. Items with a smaller value of iseq are run earlier at shutdown. Suitable sequence values are given by HUB_SEQUENCE and CLIENT_SEQUENCE. The key 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 later
      iseq - value indicating position in shutdown sequence
      runnable - to be run on shutdown
    • unregisterHook

      public void unregisterHook(Object key)
      Unregisters a key earlier registered using registerHook(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(Logger logger, Level level, String msg, 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 - logger
      level - level of message to log
      msg - text of logging message
      error - associated throwable if any; may be null