001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.util;
018
019import java.util.concurrent.ExecutorService;
020import java.util.concurrent.TimeUnit;
021
022import org.apache.logging.log4j.Logger;
023import org.apache.logging.log4j.status.StatusLogger;
024
025public class ExecutorServices {
026
027    private static final Logger LOGGER = StatusLogger.getLogger();
028
029    /**
030     * Shuts down the given {@link ExecutorService} in an orderly fashion. Disables new tasks from submission and then
031     * waits for existing tasks to terminate. Eventually cancels running tasks if too much time elapses.
032     * <p>
033     * If the timeout is 0, then a plain shutdown takes place.
034     * </p>
035     *
036     * @param executorService
037     *            the pool to shutdown.
038     * @param timeout
039     *            the maximum time to wait, or 0 to not wait for existing tasks to terminate.
040     * @param timeUnit
041     *            the time unit of the timeout argument
042     * @param source
043     *            use this string in any log messages.
044     * @return {@code true} if the given executor terminated and {@code false} if the timeout elapsed before
045     *         termination.
046     */
047    public static boolean shutdown(final ExecutorService executorService, final long timeout, final TimeUnit timeUnit, final String source) {
048        if (executorService == null || executorService.isTerminated()) {
049            return true;
050        }
051        executorService.shutdown(); // Disable new tasks from being submitted
052        if (timeout > 0 && timeUnit == null) {
053            throw new IllegalArgumentException(
054                    String.format("%s can't shutdown %s when timeout = %,d and timeUnit = %s.", source, executorService,
055                            timeout, timeUnit));
056        }
057        if (timeout > 0) {
058            try {
059                // Wait a while for existing tasks to terminate
060                if (!executorService.awaitTermination(timeout, timeUnit)) {
061                    executorService.shutdownNow(); // Cancel currently executing tasks
062                    // Wait a while for tasks to respond to being cancelled
063                    if (!executorService.awaitTermination(timeout, timeUnit)) {
064                        LOGGER.error("{} pool {} did not terminate after {} {}", source, executorService, timeout,
065                                timeUnit);
066                    }
067                    return false;
068                }
069            } catch (final InterruptedException ie) {
070                // (Re-)Cancel if current thread also interrupted
071                executorService.shutdownNow();
072                // Preserve interrupt status
073                Thread.currentThread().interrupt();
074            }
075        } else {
076            executorService.shutdown();
077        }
078        return true;
079    }
080
081    /** no-op method which can be invoked to ensure this class has been initialized per jls-12.4.2. */
082    public static void ensureInitialized() {}
083}