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.
  • Field Details

    • SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS

      private static final int SLEEP_MILLIS_BETWEEN_DRAIN_ATTEMPTS
      See Also:
    • MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN

      private static final int MAX_DRAIN_ATTEMPTS_BEFORE_SHUTDOWN
      See Also:
    • queueFullEnqueueLock

      private final Object queueFullEnqueueLock
    • disruptor

      private volatile com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent> disruptor
    • contextName

      private String contextName
    • useThreadLocalTranslator

      private boolean useThreadLocalTranslator
    • backgroundThreadId

      private long backgroundThreadId
    • asyncQueueFullPolicy

      private AsyncQueueFullPolicy asyncQueueFullPolicy
    • ringBufferSize

      private int ringBufferSize
  • Constructor Details

    • AsyncLoggerDisruptor

      AsyncLoggerDisruptor(String contextName)
  • Method Details

    • getContextName

      public String getContextName()
    • setContextName

      public void setContextName(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 interface LifeCycle
      Overrides:
      start in class AbstractLifeCycle
      See Also:
    • stop

      public boolean stop(long timeout, 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 to null.
      Specified by:
      stop in interface LifeCycle2
      Overrides:
      stop in class AbstractLifeCycle
      Parameters:
      timeout - the maximum time to wait
      timeUnit - 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)
      Returns true if the specified disruptor still has unprocessed events.
    • createRingBufferAdmin

      public RingBufferAdmin createRingBufferAdmin(String jmxContextName)
      Creates and returns a new RingBufferAdmin that instruments the ringbuffer of the AsyncLogger.
      Parameters:
      jmxContextName - name of the AsyncLoggerContext
      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)
      Returns true 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, StackTraceElement location, String fqcn, Level level, Marker marker, Message msg, Throwable thrown)
    • synchronizeEnqueueWhenQueueFull

      private boolean synchronizeEnqueueWhenQueueFull()
    • logWarningOnNpeFromDisruptorPublish

      private void logWarningOnNpeFromDisruptorPublish(RingBufferLogEventTranslator translator)
    • logWarningOnNpeFromDisruptorPublish

      private void logWarningOnNpeFromDisruptorPublish(Level level, String fqcn, Message msg, 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:
    • 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: