You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2016/09/05 19:59:07 UTC

logging-log4j2 git commit: Refactor ExecutorService shutdown into a class for reuse.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 0388e2bd6 -> 221b68dc7


Refactor ExecutorService shutdown into a class for reuse.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/221b68dc
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/221b68dc
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/221b68dc

Branch: refs/heads/master
Commit: 221b68dc7bbe6e704864b50dc44d5207ca655ba4
Parents: 0388e2bd
Author: Gary Gregory <gg...@apache.org>
Authored: Mon Sep 5 15:59:04 2016 -0400
Committer: Gary Gregory <gg...@apache.org>
Committed: Mon Sep 5 15:59:04 2016 -0400

----------------------------------------------------------------------
 .../logging/log4j/core/LoggerContext.java       | 45 ++--------------
 .../log4j/core/util/ExecutorServices.java       | 54 ++++++++++++++++++++
 2 files changed, 58 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/221b68dc/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
index 9918713..4b3ed88 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
@@ -45,6 +45,7 @@ import org.apache.logging.log4j.core.config.Reconfigurable;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.core.jmx.Server;
 import org.apache.logging.log4j.core.util.Cancellable;
+import org.apache.logging.log4j.core.util.ExecutorServices;
 import org.apache.logging.log4j.core.util.Log4jThreadFactory;
 import org.apache.logging.log4j.core.util.NetUtils;
 import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
@@ -332,9 +333,10 @@ public class LoggerContext extends AbstractLifeCycle
             prev.stop();
             externalContext = null;
             LogManager.getFactory().removeContext(this);
-            shutdownEs = shutdown(executorService, timeout, timeUnit);
+            final String source = "LoggerContext \'" + getName() + "\'";
+            shutdownEs = ExecutorServices.shutdown(executorService, timeout, timeUnit, source);
             // Do not wait for daemon threads
-            shutdownEsd = shutdown(executorServiceDeamons, 0, null);
+            shutdownEsd = ExecutorServices.shutdown(executorServiceDeamons, 0, null, source);
             this.setStopped();
         } finally {
             configLock.unlock();
@@ -344,45 +346,6 @@ public class LoggerContext extends AbstractLifeCycle
     }
 
     /**
-     * Shuts down the given pool.
-     * 
-     * @param pool
-     *            the pool to shutdown.
-     * @param timeout
-     *            the maximum time to wait
-     * @param unit
-     *            the time unit of the timeout argument
-     * @return {@code true} if the given executor terminated and {@code false} if the timeout elapsed before termination.
-     */
-    private boolean shutdown(ExecutorService pool, long timeout, TimeUnit timeUnit) {
-        pool.shutdown(); // Disable new tasks from being submitted
-        if (timeout > 0 && timeUnit == null) {
-            throw new IllegalArgumentException(
-                    String.format("LoggerContext '%s' can't shutdown %s when timeout = %,d and timeUnit = %s.",
-                            getName(), 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("LoggerContext '{}' pool {} did not terminate after {} {}", getName(), 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;
-    }
-
-    /**
      * Gets the name.
      *
      * @return the name.

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/221b68dc/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ExecutorServices.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ExecutorServices.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ExecutorServices.java
new file mode 100644
index 0000000..8f2c177
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ExecutorServices.java
@@ -0,0 +1,54 @@
+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;
+    }
+
+}