Skip to content

Commit 38e162a

Browse files
committed
Fix problems from apache#2112 reviews
1 parent 5068a03 commit 38e162a

File tree

5 files changed

+48
-37
lines changed

5 files changed

+48
-37
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public String toString() {
9393

9494
/**
9595
* EventHandler performs the work in a separate thread.
96+
* <p>
97+
* <strong>Warning:</strong> this implementation only works with Disruptor 4.x.
98+
* </p>
9699
*/
97100
private static class Log4jEventWrapperHandler implements EventHandler<Log4jEventWrapper> {
98101
private static final int NOTIFY_PROGRESS_THRESHOLD = 50;
@@ -129,7 +132,10 @@ private void notifyIntermediateProgress(final long sequence) {
129132
}
130133

131134
/**
132-
* A version of Log4jEventWrapperHandler for LMAX Disruptor 3.x.
135+
* EventHandler performs the work in a separate thread.
136+
* <p>
137+
* <strong>Warning:</strong> this implementation only works with Disruptor 3.x.
138+
* </p>
133139
*/
134140
private static final class Log4jEventWrapperHandler3 extends Log4jEventWrapperHandler
135141
implements SequenceReportingEventHandler<Log4jEventWrapper> {}
@@ -166,11 +172,13 @@ private static final class Log4jEventWrapperHandler3 extends Log4jEventWrapperHa
166172
};
167173

168174
private Log4jEventWrapperHandler createEventHandler() {
169-
try {
170-
return LoaderUtil.newInstanceOf(
171-
"org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler3");
172-
} catch (final ReflectiveOperationException | LinkageError e) {
173-
LOGGER.debug("LMAX Disruptor 3.x is missing, trying version 4.x.", e);
175+
if (DisruptorUtil.DISRUPTOR_MAJOR_VERSION == 3) {
176+
try {
177+
return LoaderUtil.newInstanceOf(
178+
"org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler3");
179+
} catch (final ReflectiveOperationException | LinkageError e) {
180+
LOGGER.warn("Failed to create event handler for LMAX Disruptor 3.x, trying version 4.x.", e);
181+
}
174182
}
175183
return new Log4jEventWrapperHandler();
176184
}

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19+
import com.lmax.disruptor.EventHandler;
1920
import com.lmax.disruptor.EventTranslatorVararg;
2021
import com.lmax.disruptor.ExceptionHandler;
2122
import com.lmax.disruptor.RingBuffer;
@@ -35,6 +36,7 @@
3536
import org.apache.logging.log4j.core.util.Log4jThreadFactory;
3637
import org.apache.logging.log4j.core.util.Throwables;
3738
import org.apache.logging.log4j.message.Message;
39+
import org.apache.logging.log4j.util.LoaderUtil;
3840

3941
/**
4042
* Helper class for async loggers: AsyncLoggerDisruptor handles the mechanics of working with the LMAX Disruptor, and
@@ -46,6 +48,20 @@ class AsyncLoggerDisruptor extends AbstractLifeCycle {
4648
private static final int SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS = 50;
4749
private static final int MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN = 200;
4850

51+
/**
52+
* Creates an appropriate event handler for the Disruptor library used.
53+
*/
54+
private static EventHandler<RingBufferLogEvent> createEventHandler() {
55+
if (DisruptorUtil.DISRUPTOR_MAJOR_VERSION == 3) {
56+
try {
57+
return LoaderUtil.newInstanceOf("org.apache.logging.log4j.core.async.RingBufferLogEventHandler");
58+
} catch (final ReflectiveOperationException | LinkageError e) {
59+
LOGGER.warn("Failed to create event handler for LMAX Disruptor 3.x, trying version 4.x.", e);
60+
}
61+
}
62+
return new RingBufferLogEventHandler4();
63+
}
64+
4965
private final Object queueFullEnqueueLock = new Object();
5066

5167
private volatile Disruptor<RingBufferLogEvent> disruptor;
@@ -122,8 +138,8 @@ public Thread newThread(final Runnable r) {
122138
final ExceptionHandler<RingBufferLogEvent> errorHandler = DisruptorUtil.getAsyncLoggerExceptionHandler();
123139
disruptor.setDefaultExceptionHandler(errorHandler);
124140

125-
final RingBufferLogEventHandler4[] handlers = {RingBufferLogEventHandler4.create()};
126-
disruptor.handleEventsWith(handlers);
141+
final EventHandler<RingBufferLogEvent> handler = createEventHandler();
142+
disruptor.handleEventsWith(handler);
127143

128144
LOGGER.debug(
129145
"[{}] Starting AsyncLogger disruptor for this context with ringbufferSize={}, waitStrategy={}, "

log4j-core/src/main/java/org/apache/logging/log4j/core/async/DefaultAsyncWaitStrategyFactory.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ static WaitStrategy createDefaultWaitStrategy(final String propertyName) {
7474
LOGGER.trace(
7575
"DefaultAsyncWaitStrategyFactory creating TimeoutBlockingWaitStrategy(timeout={}, unit=MILLIS)",
7676
timeoutMillis);
77-
try {
78-
// Check for the v 4.x version of the strategy, the version in 3.x is not garbage-free.
79-
if (DisruptorUtil.DISRUPTOR_MAJOR_VERSION == 4) {
77+
// Check for the v 4.x version of the strategy, the version in 3.x is not garbage-free.
78+
if (DisruptorUtil.DISRUPTOR_MAJOR_VERSION == 4) {
79+
try {
8080
return (WaitStrategy) Class.forName("com.lmax.disruptor.TimeoutBlockingWaitStrategy")
8181
.getConstructor(long.class, TimeUnit.class)
8282
.newInstance(timeoutMillis, TimeUnit.MILLISECONDS);
83+
} catch (final ReflectiveOperationException | LinkageError e) {
84+
LOGGER.debug(
85+
"DefaultAsyncWaitStrategyFactory failed to load 'com.lmax.disruptor.TimeoutBlockingWaitStrategy', using '{}' instead.",
86+
TimeoutBlockingWaitStrategy.class.getName());
8387
}
84-
} catch (final ReflectiveOperationException | LinkageError e) {
85-
LOGGER.debug(
86-
"DefaultAsyncWaitStrategyFactory failed to load 'com.lmax.disruptor.TimeoutBlockingWaitStrategy', using '{}' instead.",
87-
TimeoutBlockingWaitStrategy.class.getName());
8888
}
8989
// Use our version
9090
return new TimeoutBlockingWaitStrategy(timeoutMillis, TimeUnit.MILLISECONDS);

log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEventHandler.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424
* available. Processing of these messages is done in a separate thread,
2525
* controlled by the {@code Executor} passed to the {@code Disruptor}
2626
* constructor.
27+
* <p>
28+
* <strong>Warning:</strong> this class only works with Disruptor 3.x.
29+
* </p>
30+
* @deprecated Only used internally, will be removed in the next major version.
2731
*/
32+
@Deprecated
2833
public class RingBufferLogEventHandler extends RingBufferLogEventHandler4
29-
implements SequenceReportingEventHandler<RingBufferLogEvent>, LifecycleAware {
30-
31-
/**
32-
* @deprecated Use the {@link RingBufferLogEventHandler4#create()} factory method instead.
33-
*/
34-
@Deprecated
35-
public RingBufferLogEventHandler() {}
36-
}
34+
implements SequenceReportingEventHandler<RingBufferLogEvent>, LifecycleAware {}

log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEventHandler4.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818

1919
import com.lmax.disruptor.EventHandler;
2020
import com.lmax.disruptor.Sequence;
21-
import org.apache.logging.log4j.status.StatusLogger;
22-
import org.apache.logging.log4j.util.LoaderUtil;
2321

2422
/**
2523
* This event handler gets passed messages from the RingBuffer as they become
2624
* available. Processing of these messages is done in a separate thread,
2725
* controlled by the {@code Executor} passed to the {@code Disruptor}
2826
* constructor.
27+
* * <p>
28+
* * <strong>Warning:</strong> this class only works with Disruptor 4.x.
29+
* * </p>
2930
*/
3031
class RingBufferLogEventHandler4 implements EventHandler<RingBufferLogEvent> {
3132

@@ -34,18 +35,6 @@ class RingBufferLogEventHandler4 implements EventHandler<RingBufferLogEvent> {
3435
private int counter;
3536
private long threadId = -1;
3637

37-
/**
38-
* Returns the appropriate {@link EventHandler} for the version of LMAX Disruptor used.
39-
*/
40-
public static RingBufferLogEventHandler4 create() {
41-
try {
42-
return LoaderUtil.newInstanceOf("org.apache.logging.log4j.core.async.RingBufferLogEventHandler");
43-
} catch (final ReflectiveOperationException | LinkageError e) {
44-
StatusLogger.getLogger().debug("LMAX Disruptor 3.x is missing, trying version 4.x.", e);
45-
}
46-
return new RingBufferLogEventHandler4();
47-
}
48-
4938
/*
5039
* Overrides a method from Disruptor 4.x. Do not remove.
5140
*/

0 commit comments

Comments
 (0)