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 AsyncQueueFullPolicyasyncQueueFullPolicyprivate longbackgroundThreadIdprivate java.lang.StringcontextNameprivate com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent>disruptorprivate static intMAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWNprivate java.lang.ObjectqueueFullEnqueueLockprivate intringBufferSizeprivate static intSLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTSprivate booleanuseThreadLocalTranslatorprivate com.lmax.disruptor.WaitStrategywaitStrategyprivate 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 RingBufferAdmincreateRingBufferAdmin(java.lang.String jmxContextName)Creates and returns a newRingBufferAdminthat instruments the ringbuffer of theAsyncLogger.(package private) voidenqueueLogMessageWhenQueueFull(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) voidenqueueLogMessageWhenQueueFull(RingBufferLogEventTranslator translator)java.lang.StringgetContextName()(package private) com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent>getDisruptor()(package private) EventRoutegetEventRoute(Level logLevel)(package private) com.lmax.disruptor.WaitStrategygetWaitStrategy()private static booleanhasBacklog(com.lmax.disruptor.dsl.Disruptor<?> theDisruptor)Returnstrueif the specified disruptor still has unprocessed events.private booleanhasLog4jBeenShutDown(com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent> aDisruptor)Returnstrueif the specified disruptor is null.booleanisUseThreadLocals()Returns whether it is allowed to store non-JDK classes in ThreadLocal objects for efficiency.private voidlogWarningOnNpeFromDisruptorPublish(RingBufferLogEventTranslator translator)private voidlogWarningOnNpeFromDisruptorPublish(Level level, java.lang.String fqcn, Message msg, java.lang.Throwable thrown)private intremainingDisruptorCapacity()voidsetContextName(java.lang.String name)voidsetUseThreadLocals(boolean allow)Signals this AsyncLoggerDisruptor whether it is allowed to store non-JDK classes in ThreadLocal objects for efficiency.voidstart()Creates and starts a new Disruptor and associated thread if none currently exists.booleanstop(long timeout, java.util.concurrent.TimeUnit timeUnit)Decreases the reference count.private booleansynchronizeEnqueueWhenQueueFull()(package private) booleantryPublish(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:
startin interfaceLifeCycle- Overrides:
startin 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:
stopin interfaceLifeCycle2- Overrides:
stopin 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)
Returnstrueif the specified disruptor still has unprocessed events.
-
createRingBufferAdmin
public RingBufferAdmin createRingBufferAdmin(java.lang.String jmxContextName)
Creates and returns a newRingBufferAdminthat instruments the ringbuffer of theAsyncLogger.- Parameters:
jmxContextName- name of theAsyncLoggerContext- Returns:
- a new
RingBufferAdminthat instruments the ringbuffer
-
getEventRoute
EventRoute getEventRoute(Level logLevel)
-
remainingDisruptorCapacity
private int remainingDisruptorCapacity()
-
hasLog4jBeenShutDown
private boolean hasLog4jBeenShutDown(com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent> aDisruptor)
Returnstrueif 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
-
-