diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java index feb0536..afb8dd0 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java @@ -156,8 +156,9 @@ public class AsyncLoggerConfigDisruptor implements AsyncLoggerConfigDelegate { return; } LOGGER.trace("AsyncLoggerConfigDisruptor creating new disruptor for this configuration."); + final long timeout = DisruptorUtil.getTimeout("AsyncLoggerConfig.Timeout", 10L); ringBufferSize = DisruptorUtil.calculateRingBufferSize("AsyncLoggerConfig.RingBufferSize"); - final WaitStrategy waitStrategy = DisruptorUtil.createWaitStrategy("AsyncLoggerConfig.WaitStrategy"); + final WaitStrategy waitStrategy = DisruptorUtil.createWaitStrategy("AsyncLoggerConfig.WaitStrategy", timeout); executor = Executors.newSingleThreadExecutor(THREAD_FACTORY); backgroundThreadId = DisruptorUtil.getExecutorThreadId(executor); asyncEventRouter = AsyncEventRouterFactory.create(ringBufferSize); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java index 1da2802..de09922 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java @@ -79,8 +79,9 @@ class AsyncLoggerDisruptor { return; } LOGGER.trace("[{}] AsyncLoggerDisruptor creating new disruptor for this context.", contextName); + final long timeout = DisruptorUtil.getTimeout("AsyncLoggerConfig.Timeout", 10L); ringBufferSize = DisruptorUtil.calculateRingBufferSize("AsyncLogger.RingBufferSize"); - final WaitStrategy waitStrategy = DisruptorUtil.createWaitStrategy("AsyncLogger.WaitStrategy"); + final WaitStrategy waitStrategy = DisruptorUtil.createWaitStrategy("AsyncLogger.WaitStrategy", timeout); executor = Executors.newSingleThreadExecutor(new DaemonThreadFactory("AsyncLogger[" + contextName + "]")); backgroundThreadId = DisruptorUtil.getExecutorThreadId(executor); asyncEventRouter = AsyncEventRouterFactory.create(ringBufferSize); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java index 917710f..d0c69ed 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java @@ -20,18 +20,14 @@ package org.apache.logging.log4j.core.async; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import com.lmax.disruptor.*; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.util.Integers; import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.PropertiesUtil; -import com.lmax.disruptor.BlockingWaitStrategy; -import com.lmax.disruptor.ExceptionHandler; -import com.lmax.disruptor.SleepingWaitStrategy; -import com.lmax.disruptor.WaitStrategy; -import com.lmax.disruptor.YieldingWaitStrategy; - /** * Utility methods for getting Disruptor related configuration. */ @@ -43,7 +39,12 @@ final class DisruptorUtil { private DisruptorUtil() { } - static WaitStrategy createWaitStrategy(final String propertyName) { + static long getTimeout(final String propertyName, final long defaultTimeout) + { + return PropertiesUtil.getProperties().getLongProperty(propertyName, defaultTimeout); + } + + static WaitStrategy createWaitStrategy(final String propertyName, final long timeoutMs) { final String strategy = PropertiesUtil.getProperties().getStringProperty(propertyName); if (strategy != null) { LOGGER.trace("property {}={}", propertyName, strategy); @@ -53,6 +54,8 @@ final class DisruptorUtil { return new YieldingWaitStrategy(); } else if ("Block".equalsIgnoreCase(strategy)) { return new BlockingWaitStrategy(); + } else if ("Timeout".equalsIgnoreCase(strategy)) { + return new TimeoutBlockingWaitStrategy(timeoutMs, TimeUnit.MILLISECONDS); } } return new BlockingWaitStrategy(); @@ -95,7 +98,7 @@ final class DisruptorUtil { /** * Returns the thread ID of the background appender thread. This allows us to detect Logger.log() calls initiated * from the appender thread, which may cause deadlock when the RingBuffer is full. (LOG4J2-471) - * + * * @param executor runs the appender thread * @return the thread ID of the background appender thread */ @@ -114,5 +117,4 @@ final class DisruptorUtil { throw new IllegalStateException(msg, ex); } } - }