Class ShutdownManager


  • public class ShutdownManager
    extends java.lang.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 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 using registerHook(java.lang.Object, int, java.lang.Runnable).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 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
      • 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 - logger
        level - level of message to log
        msg - text of logging message
        error - associated throwable if any; may be null