Class AsyncLoggerDisruptor
- java.lang.Object
-
- org.apache.logging.log4j.core.AbstractLifeCycle
-
- org.apache.logging.log4j.core.async.AsyncLoggerDisruptor
-
- All Implemented Interfaces:
LifeCycle
,LifeCycle2
class AsyncLoggerDisruptor extends AbstractLifeCycle
Helper class for async loggers: AsyncLoggerDisruptor handles the mechanics of working with the LMAX Disruptor, and works with its associated AsyncLoggerContext to synchronize the life cycle of the Disruptor and its thread with the life cycle of the context. The AsyncLoggerDisruptor of the context is shared by all AsyncLogger objects created by that AsyncLoggerContext.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle
LifeCycle.State
-
-
Field Summary
Fields Modifier and Type Field Description private AsyncQueueFullPolicy
asyncQueueFullPolicy
private long
backgroundThreadId
private java.lang.String
contextName
private com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent>
disruptor
private static int
MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN
private java.lang.Object
queueFullEnqueueLock
private int
ringBufferSize
private static int
SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS
private boolean
useThreadLocalTranslator
private com.lmax.disruptor.WaitStrategy
waitStrategy
private java.util.function.Supplier<AsyncWaitStrategyFactory>
waitStrategyFactorySupplier
-
Fields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER
-
-
Constructor Summary
Constructors Constructor Description AsyncLoggerDisruptor(java.lang.String contextName, java.util.function.Supplier<AsyncWaitStrategyFactory> waitStrategyFactorySupplier)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RingBufferAdmin
createRingBufferAdmin(java.lang.String jmxContextName)
Creates and returns a newRingBufferAdmin
that instruments the ringbuffer of theAsyncLogger
.(package private) void
enqueueLogMessageWhenQueueFull(com.lmax.disruptor.EventTranslatorVararg<RingBufferLogEvent> translator, AsyncLogger asyncLogger, java.lang.StackTraceElement location, java.lang.String fqcn, Level level, Marker marker, Message msg, java.lang.Throwable thrown)
(package private) void
enqueueLogMessageWhenQueueFull(RingBufferLogEventTranslator translator)
java.lang.String
getContextName()
(package private) com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent>
getDisruptor()
(package private) EventRoute
getEventRoute(Level logLevel)
(package private) com.lmax.disruptor.WaitStrategy
getWaitStrategy()
private static boolean
hasBacklog(com.lmax.disruptor.dsl.Disruptor<?> theDisruptor)
Returnstrue
if the specified disruptor still has unprocessed events.private boolean
hasLog4jBeenShutDown(com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent> aDisruptor)
Returnstrue
if the specified disruptor is null.boolean
isUseThreadLocals()
Returns whether it is allowed to store non-JDK classes in ThreadLocal objects for efficiency.private void
logWarningOnNpeFromDisruptorPublish(RingBufferLogEventTranslator translator)
private void
logWarningOnNpeFromDisruptorPublish(Level level, java.lang.String fqcn, Message msg, java.lang.Throwable thrown)
private int
remainingDisruptorCapacity()
void
setContextName(java.lang.String name)
void
setUseThreadLocals(boolean allow)
Signals this AsyncLoggerDisruptor whether it is allowed to store non-JDK classes in ThreadLocal objects for efficiency.void
start()
Creates and starts a new Disruptor and associated thread if none currently exists.boolean
stop(long timeout, java.util.concurrent.TimeUnit timeUnit)
Decreases the reference count.private boolean
synchronizeEnqueueWhenQueueFull()
(package private) boolean
tryPublish(RingBufferLogEventTranslator translator)
-
Methods inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
equalsImpl, getState, getStatusLogger, hashCodeImpl, initialize, isInitialized, isStarted, isStarting, isStopped, isStopping, setStarted, setStarting, setState, setStopped, setStopping, stop, stop
-
-
-
-
Field Detail
-
SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS
private static final int SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS
- See Also:
- Constant Field Values
-
MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN
private static final int MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN
- See Also:
- Constant Field Values
-
queueFullEnqueueLock
private final java.lang.Object queueFullEnqueueLock
-
disruptor
private volatile com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent> disruptor
-
contextName
private java.lang.String contextName
-
waitStrategyFactorySupplier
private final java.util.function.Supplier<AsyncWaitStrategyFactory> waitStrategyFactorySupplier
-
useThreadLocalTranslator
private boolean useThreadLocalTranslator
-
backgroundThreadId
private long backgroundThreadId
-
asyncQueueFullPolicy
private AsyncQueueFullPolicy asyncQueueFullPolicy
-
ringBufferSize
private int ringBufferSize
-
waitStrategy
private com.lmax.disruptor.WaitStrategy waitStrategy
-
-
Constructor Detail
-
AsyncLoggerDisruptor
AsyncLoggerDisruptor(java.lang.String contextName, java.util.function.Supplier<AsyncWaitStrategyFactory> waitStrategyFactorySupplier)
-
-
Method Detail
-
getWaitStrategy
com.lmax.disruptor.WaitStrategy getWaitStrategy()
-
getContextName
public java.lang.String getContextName()
-
setContextName
public void setContextName(java.lang.String name)
-
getDisruptor
com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent> getDisruptor()
-
start
public void start()
Creates and starts a new Disruptor and associated thread if none currently exists.- Specified by:
start
in interfaceLifeCycle
- Overrides:
start
in classAbstractLifeCycle
- See Also:
AbstractLifeCycle.stop()
-
stop
public boolean stop(long timeout, java.util.concurrent.TimeUnit timeUnit)
Decreases the reference count. If the reference count reached zero, the Disruptor and its associated thread are shut down and their references set tonull
.- Specified by:
stop
in interfaceLifeCycle2
- Overrides:
stop
in classAbstractLifeCycle
- Parameters:
timeout
- the maximum time to waittimeUnit
- the time unit of the timeout argument- Returns:
- true if the receiver was stopped cleanly and normally, false otherwise.
-
hasBacklog
private static boolean hasBacklog(com.lmax.disruptor.dsl.Disruptor<?> theDisruptor)
Returnstrue
if the specified disruptor still has unprocessed events.
-
createRingBufferAdmin
public RingBufferAdmin createRingBufferAdmin(java.lang.String jmxContextName)
Creates and returns a newRingBufferAdmin
that instruments the ringbuffer of theAsyncLogger
.- Parameters:
jmxContextName
- name of theAsyncLoggerContext
- Returns:
- a new
RingBufferAdmin
that instruments the ringbuffer
-
getEventRoute
EventRoute getEventRoute(Level logLevel)
-
remainingDisruptorCapacity
private int remainingDisruptorCapacity()
-
hasLog4jBeenShutDown
private boolean hasLog4jBeenShutDown(com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent> aDisruptor)
Returnstrue
if the specified disruptor is null.
-
tryPublish
boolean tryPublish(RingBufferLogEventTranslator translator)
-
enqueueLogMessageWhenQueueFull
void enqueueLogMessageWhenQueueFull(RingBufferLogEventTranslator translator)
-
enqueueLogMessageWhenQueueFull
void enqueueLogMessageWhenQueueFull(com.lmax.disruptor.EventTranslatorVararg<RingBufferLogEvent> translator, AsyncLogger asyncLogger, java.lang.StackTraceElement location, java.lang.String fqcn, Level level, Marker marker, Message msg, java.lang.Throwable thrown)
-
synchronizeEnqueueWhenQueueFull
private boolean synchronizeEnqueueWhenQueueFull()
-
logWarningOnNpeFromDisruptorPublish
private void logWarningOnNpeFromDisruptorPublish(RingBufferLogEventTranslator translator)
-
logWarningOnNpeFromDisruptorPublish
private void logWarningOnNpeFromDisruptorPublish(Level level, java.lang.String fqcn, Message msg, java.lang.Throwable thrown)
-
isUseThreadLocals
public boolean isUseThreadLocals()
Returns whether it is allowed to store non-JDK classes in ThreadLocal objects for efficiency.- Returns:
- whether AsyncLoggers are allowed to use ThreadLocal objects
- Since:
- 2.5
- See Also:
- LOG4J2-1172
-
setUseThreadLocals
public void setUseThreadLocals(boolean allow)
Signals this AsyncLoggerDisruptor whether it is allowed to store non-JDK classes in ThreadLocal objects for efficiency.This property may be modified after the
start()
method has been called.- Parameters:
allow
- whether AsyncLoggers are allowed to use ThreadLocal objects- Since:
- 2.5
- See Also:
- LOG4J2-1172
-
-