package org.apache.logging.log4j.core.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.status.StatusLogger;

public class ExecutorServices {

    private static final Logger LOGGER = StatusLogger.getLogger();
    
    /**
     * Shuts down the given pool.
     * 
     * @param pool
     *            the pool to shutdown.
     * @param timeout
     *            the maximum time to wait
     * @param source 
     *            use this string in any log messages.
     * @param timeUnit
     *            the time unit of the timeout argument
     * @return {@code true} if the given executor terminated and {@code false} if the timeout elapsed before termination.
     */
    public static boolean shutdown(ExecutorService pool, long timeout, TimeUnit timeUnit, String source) {
        pool.shutdown(); // Disable new tasks from being submitted
        if (timeout > 0 && timeUnit == null) {
            throw new IllegalArgumentException(
                    String.format("%s can't shutdown %s when timeout = %,d and timeUnit = %s.",
                            source, pool, timeout, timeUnit));
        }
        if (timeout > 0) {
            try {
                // Wait a while for existing tasks to terminate
                if (!pool.awaitTermination(timeout, timeUnit)) {
                    pool.shutdownNow(); // Cancel currently executing tasks
                    // Wait a while for tasks to respond to being cancelled
                    if (!pool.awaitTermination(timeout, timeUnit)) {
                        LOGGER.error("{} pool {} did not terminate after {} {}", source, pool, timeout, timeUnit);
                    }
                    return false;
                }
            } catch (InterruptedException ie) {
                // (Re-)Cancel if current thread also interrupted
                pool.shutdownNow();
                // Preserve interrupt status
                Thread.currentThread().interrupt();
            }
        }
        return true;
    }

}

@@ -1,8 +1,809 @@
+/*%0A * Licensed to the Apache Software Foundation (ASF) under one or more%0A * contributor license agreements. See the NOTICE file distributed with%0A * this work for additional information regarding copyright ownership.%0A * The ASF licenses this file to You under the Apache license, Version 2.0%0A * (the %22License%22); you may not use this file except in compliance with%0A * the License. You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the license for the specific language governing permissions and%0A * limitations under the license.%0A */%0A
 package 
@@ -1117,20 +1117,16 @@
 gger();%0A
-    
 %0A    /**
@@ -1154,20 +1154,210 @@
 e given 
-pool
+%7B@link ExecutorService%7D in an orderly fashion. Disables new tasks from submission and then%0A     * waits for existing tasks to terminate. Eventually cancels running tasks if too much time elapses
 .%0A     *
@@ -1372,20 +1372,31 @@
  @param 
-pool
+executorService
 %0A     * 
@@ -1513,17 +1513,16 @@
 m source
- 
 %0A     * 
@@ -1756,16 +1756,31 @@
 d before
+%0A     *        
  termina
@@ -1844,20 +1844,31 @@
 Service 
-pool
+executorService
 , long t
@@ -1911,36 +1911,47 @@
 urce) %7B%0A        
-pool
+executorService
 .shutdown(); // 
@@ -2179,16 +2179,41 @@
  = %25s.%22,
+ source, executorService,
 %0A       
@@ -2232,30 +2232,16 @@
         
- source, pool,
  timeout
@@ -2386,36 +2386,47 @@
            if (!
-pool
+executorService
 .awaitTerminatio
@@ -2461,36 +2461,47 @@
                 
-pool
+executorService
 .shutdownNow(); 
@@ -2637,20 +2637,31 @@
    if (!
-pool
+executorService
 .awaitTe
@@ -2785,22 +2785,65 @@
 ce, 
-pool, timeout,
+executorService, timeout,%0A                               
  tim
@@ -3054,20 +3054,31 @@
         
-pool
+executorService
 .shutdow

Refactor ExecutorService shutdown into a class for reuse.