Class GracefulExecutorServicesShutdown


  • public class GracefulExecutorServicesShutdown
    extends java.lang.Object
    This a builder like class for providing a fluent API for graceful shutdown Executors are added with the shutdown(ExecutorService...) method. The ExecutorService.shutdown() method is called immediately. Calling the handle() method returns a future which completes when all executors have been terminated. The executors will waited for completion with the ExecutorService.awaitTermination(long, TimeUnit) method. If the shutdown times out or the future is cancelled, all executors will be terminated and the termination timeout value will be used for waiting for termination. The default value for termination timeout is 10% of the shutdown timeout.
    • Method Detail

      • initiate

        public static GracefulExecutorServicesShutdown initiate()
        Initiates a new shutdown for one or many ExecutorServices.
        Returns:
        a new instance for controlling graceful shutdown
      • shutdown

        public GracefulExecutorServicesShutdown shutdown​(java.util.concurrent.ExecutorService... executorServices)
        Calls ExecutorService.shutdown() and enlists the executor as part of the shutdown handling.
        Parameters:
        executorServices - one or many executors to shutdown
        Returns:
        the current instance for controlling graceful shutdown
      • timeout

        public GracefulExecutorServicesShutdown timeout​(java.time.Duration timeout)
        Sets the timeout for graceful shutdown.
        Parameters:
        timeout - duration for the timeout
        Returns:
        the current instance for controlling graceful shutdown
      • terminationTimeout

        public GracefulExecutorServicesShutdown terminationTimeout​(java.time.Duration terminationTimeout)
        Sets the timeout for waiting for executors to complete in forceful termination.
        Parameters:
        terminationTimeout - duration for the timeout
        Returns:
        the current instance for controlling graceful shutdown
      • handle

        public java.util.concurrent.CompletableFuture<java.lang.Void> handle()
        Starts the handler for polling frequently for the completed termination of enlisted executors. If the termination times out or the future is cancelled, all active executors will be forcefully terminated by calling ExecutorService.shutdownNow().
        Returns:
        a future which completes when all executors have terminated