Class 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 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
      • useThreadLocalTranslator

        private boolean useThreadLocalTranslator
      • backgroundThreadId

        private long backgroundThreadId
      • ringBufferSize

        private int ringBufferSize
    • Constructor Detail

      • AsyncLoggerDisruptor

        AsyncLoggerDisruptor​(java.lang.String contextName)
    • Method Detail

      • getContextName

        public java.lang.String getContextName()
      • setContextName

        public void setContextName​(java.lang.String name)
      • 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 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​(java.lang.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
      • remainingDisruptorCapacity

        private int remainingDisruptorCapacity()
      • hasLog4jBeenShutDown

        private boolean hasLog4jBeenShutDown​(com.lmax.disruptor.dsl.Disruptor<RingBufferLogEvent> aDisruptor)
        Returns true if the specified disruptor is null.
      • 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​(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