You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2016/09/04 18:38:25 UTC

[09/36] logging-log4j2 git commit: Make AsyncAppender BlockingQueueFactory implementation configurable

Make AsyncAppender BlockingQueueFactory implementation configurable

Part of LOG4J2-1430


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

Branch: refs/heads/master
Commit: 4819624663ceaaeffdeeda7e9e9bf82783fcbb90
Parents: b0a3bb4
Author: Matt Sicker <bo...@gmail.com>
Authored: Wed Jun 15 22:42:08 2016 -0500
Committer: Matt Sicker <bo...@gmail.com>
Committed: Wed Jun 15 22:42:08 2016 -0500

----------------------------------------------------------------------
 .../log4j/core/appender/AsyncAppender.java      |  2 +-
 .../log4j/core/util/BlockingQueueFactory.java   |  3 ++
 .../core/util/BlockingQueueFactoryUtil.java     | 42 ++++++++------------
 .../log4j/core/appender/AsyncAppenderTest.java  | 26 ++++++++++--
 4 files changed, 43 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/48196246/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java
index 70edc19..a71c4b1 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AsyncAppender.java
@@ -75,7 +75,7 @@ public final class AsyncAppender extends AbstractAppender {
                           final boolean ignoreExceptions,
                           final long shutdownTimeout, final Configuration config, final boolean includeLocation) {
         super(name, filter, null, ignoreExceptions);
-        this.queue = BlockingQueueFactoryUtil.getLogEventBlockingQueueFactory().create(queueSize);
+        this.queue = BlockingQueueFactoryUtil.<LogEvent>getBlockingQueueFactory().create(queueSize);
         this.queueSize = queueSize;
         this.blocking = blocking;
         this.shutdownTimeout = shutdownTimeout;

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/48196246/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactory.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactory.java
index 9251799..6cfdd14 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactory.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactory.java
@@ -8,5 +8,8 @@ import java.util.concurrent.BlockingQueue;
  * @since 2.7
  */
 public interface BlockingQueueFactory<E> {
+
+    String PROPERTY = "log4j.BlockingQueueFactory";
+
     BlockingQueue<E> create(int capacity);
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/48196246/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactoryUtil.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactoryUtil.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactoryUtil.java
index 7818157..ebef319 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactoryUtil.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BlockingQueueFactoryUtil.java
@@ -3,52 +3,42 @@ package org.apache.logging.log4j.core.util;
 import java.lang.reflect.InvocationTargetException;
 
 import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.LoaderUtil;
 
 /**
- * Utility class for obtaining a {@link BlockingQueueFactory}. If the Conversant Disruptor library is available, then
- * {@link DisruptorBlockingQueueFactory} will be used; otherwise, {@link ArrayBlockingQueueFactory} will be used.
+ * Utility class for obtaining a {@link BlockingQueueFactory}. By default, {@link ArrayBlockingQueueFactory} is used,
+ * but this can be overridden by the system property {@code log4j.BlockingQueueFactory}.
  *
  * @since 2.7
  */
 public final class BlockingQueueFactoryUtil {
 
     private static final Logger LOGGER = StatusLogger.getLogger();
-    private static final BlockingQueueFactory<LogEvent> LOG_EVENT_BLOCKING_QUEUE_FACTORY;
 
-    static {
-        BlockingQueueFactory<LogEvent> factory = null;
-        if (LoaderUtil.isClassAvailable("com.conversantmedia.util.concurrent.DisruptorBlockingQueue")) {
-            try {
-                factory = newDisruptorBlockingQueueFactory();
-            } catch (final ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
-                LOGGER.warn("Found Conversant Disruptor, but an error is preventing use of it." +
-                    " Falling back to default ArrayBlockingQueue implementation.", e);
-            }
+    /**
+     * Returns a new BlockingQueueFactory.
+     *
+     * @return a BlockingQueueFactory instance
+     */
+    public static <E> BlockingQueueFactory<E> getBlockingQueueFactory() {
+        BlockingQueueFactory<E> factory = null;
+        try {
+            factory = newBlockingQueueFactory();
+        } catch (final ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
+            LOGGER.error("Specified log4j.BlockingQueueFactory could not be instantiated.", e);
         }
         if (factory == null) {
             factory = new ArrayBlockingQueueFactory<>();
         }
-        LOG_EVENT_BLOCKING_QUEUE_FACTORY = factory;
+        return factory;
     }
 
     @SuppressWarnings("unchecked")
-    private static BlockingQueueFactory<LogEvent> newDisruptorBlockingQueueFactory()
+    private static <E> BlockingQueueFactory<E> newBlockingQueueFactory()
         throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException,
         InvocationTargetException {
-        return LoaderUtil.newCheckedInstanceOf("org.apache.logging.log4j.core.util.DisruptorBlockingQueueFactory",
-            BlockingQueueFactory.class);
-    }
-
-    /**
-     * Returns a suitable BlockingQueueFactory for LogEvents.
-     *
-     * @return a BlockingQueueFactory instance
-     */
-    public static BlockingQueueFactory<LogEvent> getLogEventBlockingQueueFactory() {
-        return LOG_EVENT_BLOCKING_QUEUE_FACTORY;
+        return LoaderUtil.newCheckedInstanceOfProperty(BlockingQueueFactory.PROPERTY, BlockingQueueFactory.class);
     }
 
     private BlockingQueueFactoryUtil() {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/48196246/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java
index 7a86e48..73ae08e 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/AsyncAppenderTest.java
@@ -21,23 +21,43 @@ import java.util.List;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.LoggingException;
+import org.apache.logging.log4j.core.util.ArrayBlockingQueueFactory;
+import org.apache.logging.log4j.core.util.BlockingQueueFactory;
+import org.apache.logging.log4j.core.util.DisruptorBlockingQueueFactory;
+import org.apache.logging.log4j.core.util.LinkedTransferQueueFactory;
 import org.apache.logging.log4j.junit.LoggerContextRule;
 import org.apache.logging.log4j.test.appender.ListAppender;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.ClassRule;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
 
 import static org.junit.Assert.*;
 
 /**
  *
  */
+@RunWith(Parameterized.class)
 public class AsyncAppenderTest {
     private static final String CONFIG = "log4j-asynch.xml";
 
-    @ClassRule
-    public static LoggerContextRule context = new LoggerContextRule(CONFIG);
+    @Parameterized.Parameters
+    public static Object[] data() {
+        return new Class<?>[]{
+            ArrayBlockingQueueFactory.class,
+            DisruptorBlockingQueueFactory.class,
+            LinkedTransferQueueFactory.class
+        };
+    }
+
+    public AsyncAppenderTest(final Class<? extends BlockingQueueFactory> factory) {
+        context = new LoggerContextRule(CONFIG).withSystemProperty(BlockingQueueFactory.PROPERTY, factory.getName());
+    }
+
+    @Rule
+    public LoggerContextRule context;
 
     private ListAppender listAppender;