Class ShutdownHookUtils

  • All Implemented Interfaces:
    Utils

    public abstract class ShutdownHookUtils
    extends java.lang.Object
    implements Utils
    Utilities for managing shutdown hooks in a JVM application.

    This class provides methods to register and manage shutdown hook callbacks, which are executed when the JVM begins its shutdown sequence. These callbacks can be used to perform cleanup operations, such as closing resources or saving state before the application exits.

    Example Usage

    Registering a Shutdown Hook

    
     ShutdownHookUtils.addShutdownHookCallback(() -> {
         System.out.println("Performing cleanup before shutdown...");
     });
     

    Filtering Existing Shutdown Hooks

    
     Set<Thread> shutdownHooks = ShutdownHookUtils.getShutdownHookThreads();
     shutdownHooks.forEach(System.out::println);
     

    Clearing All Registered Callbacks

    
     ShutdownHookUtils.clearShutdownHookCallbacks();
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Runtime.addShutdownHook(Thread), java.lang.ApplicationShutdownHooks, ShutdownHookCallbacksThread
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean addShutdownHookCallback​(java.lang.Runnable callback)
      Adds a shutdown hook callback to be executed during JVM shutdown.
      static java.util.Set<java.lang.Thread> filterShutdownHookThreads​(java.util.function.Predicate<? super java.lang.Thread> hookThreadFilter)
      Filters and returns a set of registered JVM shutdown hook threads based on the provided predicate.
      static java.util.Set<java.lang.Thread> filterShutdownHookThreads​(java.util.function.Predicate<? super java.lang.Thread> hookThreadFilter, boolean removed)
      Filters and returns a set of registered JVM shutdown hook threads based on the provided predicate.
      static java.util.Queue<java.lang.Runnable> getShutdownHookCallbacks()
      Retrieves an unmodifiable Queue containing all registered shutdown hook callbacks.
      static java.util.Set<java.lang.Thread> getShutdownHookThreads()
      Retrieves an unmodifiable set of all registered JVM shutdown hook threads.
      static void registerShutdownHook()
      Registers the ShutdownHookCallbacksThread as a JVM shutdown hook to execute registered callbacks during application shutdown.
      static boolean removeShutdownHookCallback​(java.lang.Runnable callback)
      Removes a previously registered shutdown hook callback from the queue.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SHUTDOWN_HOOK_CALLBACKS_CAPACITY_PROPERTY_NAME

        public static final java.lang.String SHUTDOWN_HOOK_CALLBACKS_CAPACITY_PROPERTY_NAME
        The System property name of the capacity of ShutdownHook callbacks
        See Also:
        Constant Field Values
      • SHUTDOWN_HOOK_CALLBACKS_CAPACITY

        public static final int SHUTDOWN_HOOK_CALLBACKS_CAPACITY
        The System property value of the capacity of ShutdownHook callbacks, the default value is 512
      • SHUTDOWN_HOOK_CALLBACKS_THREAD_FILTER

        public static final java.util.function.Predicate<? super java.lang.Thread> SHUTDOWN_HOOK_CALLBACKS_THREAD_FILTER
        The Predicate to filter the type that is ShutdownHookCallbacksThread
    • Method Detail

      • registerShutdownHook

        public static void registerShutdownHook()
        Registers the ShutdownHookCallbacksThread as a JVM shutdown hook to execute registered callbacks during application shutdown.

        If no such hook has been previously registered, this method adds the hook using Runtime.addShutdownHook(Thread).

        Example Usage

        
         // Ensure the shutdown hook is registered
         ShutdownHookUtils.registerShutdownHook();
         
      • getShutdownHookThreads

        @Nonnull
        public static java.util.Set<java.lang.Thread> getShutdownHookThreads()
        Retrieves an unmodifiable set of all registered JVM shutdown hook threads.

        This method provides access to the current collection of threads that have been registered as JVM shutdown hooks. These hooks are typically added using Runtime.addShutdownHook(Thread). The returned set reflects the current state and includes all known shutdown hook threads.

        Example Usage

        Listing All Shutdown Hook Threads

        
         Set<Thread> shutdownHooks = ShutdownHookUtils.getShutdownHookThreads();
         System.out.println("Registered shutdown hook threads:");
         shutdownHooks.forEach(System.out::println);
         

        Checking for Specific Shutdown Hooks

        
         Set<Thread> shutdownHooks = ShutdownHookUtils.getShutdownHookThreads();
         boolean hasMyHook = shutdownHooks.contains(myCustomShutdownHookThread);
         if (hasMyHook) {
             System.out.println("My custom shutdown hook is already registered.");
         }
         
        Returns:
        A non-null, unmodifiable set containing all currently registered shutdown hook threads.
      • filterShutdownHookThreads

        public static java.util.Set<java.lang.Thread> filterShutdownHookThreads​(java.util.function.Predicate<? super java.lang.Thread> hookThreadFilter)
        Filters and returns a set of registered JVM shutdown hook threads based on the provided predicate.

        This method allows filtering of shutdown hook threads by applying a given condition (predicate). The returned set contains only those threads that match the filter criteria. It provides a way to selectively retrieve specific shutdown hooks, such as identifying custom shutdown hook threads.

        Example Usage

        Filtering MicroSphere Shutdown Hook Threads

        
         Set<Thread> microsphereShutdownHooks = ShutdownHookUtils.filterShutdownHookThreads(SHUTDOWN_HOOK_CALLBACKS_THREAD_FILTER);
         System.out.println("MicroSphere shutdown hooks:");
         microsphereShutdownHooks.forEach(System.out::println);
         

        Filtering Custom Shutdown Hooks

        
         Predicate<Thread> myCustomHookPredicate = t -> t.getName().contains("MyCustomHook");
         Set<Thread> customShutdownHooks = ShutdownHookUtils.filterShutdownHookThreads(myCustomHookPredicate);
         System.out.println("Custom shutdown hooks:");
         customShutdownHooks.forEach(System.out::println);
         
        Parameters:
        hookThreadFilter - A Predicate used to filter which shutdown hook threads should be included in the result. Only threads for which the predicate returns true will be included.
        Returns:
        A non-null, unmodifiable set containing the filtered shutdown hook threads.
      • filterShutdownHookThreads

        @Nonnull
        public static java.util.Set<java.lang.Thread> filterShutdownHookThreads​(java.util.function.Predicate<? super java.lang.Thread> hookThreadFilter,
                                                                                boolean removed)
        Filters and returns a set of registered JVM shutdown hook threads based on the provided predicate.

        This method allows filtering of shutdown hook threads by applying a given condition (predicate). The returned set contains only those threads that match the filter criteria. If the removed flag is set to true, matching threads will be removed from the internal registry after being collected.

        Example Usage

        Filtering and Retaining MicroSphere Shutdown Hook Threads

        
         Set<Thread> microsphereShutdownHooks = ShutdownHookUtils.filterShutdownHookThreads(
             ShutdownHookCallbacksThread.class::isInstance, false);
         System.out.println("MicroSphere shutdown hooks:");
         microsphereShutdownHooks.forEach(System.out::println);
         

        Filtering and Removing Custom Shutdown Hooks

        
         Predicate<Thread> myCustomHookPredicate = t -> t.getName().contains("MyCustomHook");
         Set<Thread> customShutdownHooks = ShutdownHookUtils.filterShutdownHookThreads(myCustomHookPredicate, true);
         System.out.println("Removed custom shutdown hooks:");
         customShutdownHooks.forEach(System.out::println);
         
        Parameters:
        hookThreadFilter - A Predicate used to filter which shutdown hook threads should be included in the result. Only threads for which the predicate returns true will be included.
        removed - If true, the filtered threads will be removed from the internal registry.
        Returns:
        A non-null, unmodifiable set containing the filtered shutdown hook threads.
      • addShutdownHookCallback

        public static boolean addShutdownHookCallback​(@Nullable
                                                      java.lang.Runnable callback)
        Adds a shutdown hook callback to be executed during JVM shutdown.

        This method registers a Runnable callback that will be invoked when the JVM begins its shutdown sequence. The callback is added to an internal queue and will be executed in the order determined by its priority. If the callback is already registered, it will not be added again.

        Example Usage

        Registering a Simple Shutdown Hook

        
         boolean registered = ShutdownHookUtils.addShutdownHookCallback(() -> {
             System.out.println("Performing cleanup before application exit...");
         });
         if (registered) {
             System.out.println("Shutdown hook successfully registered.");
         }
         

        Registering a Prioritized Shutdown Hook

        
         boolean registered = ShutdownHookUtils.addShutdownHookCallback(new PrioritizedRunnable(() -> {
             System.out.println("High-priority cleanup task.");
         }, 100));
         
        Parameters:
        callback - the Runnable callback to be executed during JVM shutdown; may be null, in which case no action is taken
        Returns:
        true if the callback was successfully added; false otherwise
      • removeShutdownHookCallback

        public static boolean removeShutdownHookCallback​(@Nullable
                                                         java.lang.Runnable callback)
        Removes a previously registered shutdown hook callback from the queue.

        This method attempts to remove the specified Runnable callback from the internal queue of shutdown hook callbacks. If the callback is not present in the queue, this method returns false and no action is taken.

        Example Usage

        Removing a Simple Shutdown Hook Callback

        
         Runnable cleanupTask = () -> System.out.println("Cleaning up resources...");
         ShutdownHookUtils.addShutdownHookCallback(cleanupTask);
        
         boolean removed = ShutdownHookUtils.removeShutdownHookCallback(cleanupTask);
         if (removed) {
             System.out.println("Cleanup task removed successfully.");
         } else {
             System.out.println("Cleanup task was not found.");
         }
         

        Removing a Prioritized Shutdown Hook Callback

        
         PrioritizedRunnable highPriorityTask = new PrioritizedRunnable(() -> {
             System.out.println("High-priority cleanup task.");
         }, 100);
        
         ShutdownHookUtils.addShutdownHookCallback(highPriorityTask);
         boolean removed = ShutdownHookUtils.removeShutdownHookCallback(highPriorityTask);
        
         if (removed) {
             System.out.println("High-priority task removed.");
         }
         
        Parameters:
        callback - the Runnable callback to be removed; may be null, in which case no action is taken
        Returns:
        true if the callback was successfully removed; false if it was not found or if it was null
      • getShutdownHookCallbacks

        @Nonnull
        public static java.util.Queue<java.lang.Runnable> getShutdownHookCallbacks()
        Retrieves an unmodifiable Queue containing all registered shutdown hook callbacks.

        These callbacks are executed in the order determined by their priority when the JVM begins its shutdown sequence. The returned queue reflects the current state and includes all known shutdown hook callbacks.

        Example Usage

        Accessing All Registered Shutdown Hook Callbacks

        
         Queue<Runnable> callbacks = ShutdownHookUtils.getShutdownHookCallbacks();
         System.out.println("Registered shutdown hook callbacks:");
         callbacks.forEach(callback -> System.out.println(callback));
         

        Checking for Specific Callbacks

        
         Queue<Runnable> callbacks = ShutdownHookUtils.getShutdownHookCallbacks();
         boolean hasMyCallback = callbacks.contains(myCustomRunnable);
         if (hasMyCallback) {
             System.out.println("My custom callback is registered.");
         }
         
        Returns:
        A non-null, unmodifiable queue containing all currently registered shutdown hook callbacks.