Class AsyncDisruptorAppender<Event extends DeferredProcessingAware,Listener extends AppenderListener<Event>>
- Type Parameters:
Event- type of event (ILoggingEventorIAccessEvent).
- All Implemented Interfaces:
Appender<Event>,ContextAware,FilterAttachable<Event>,LifeCycle
- Direct Known Subclasses:
AbstractLogstashTcpSocketAppender,DelegatingAsyncDisruptorAppender
RingBuffer
as the interthread data exchange mechanism (as opposed to a BlockingQueue
used by logback's AsyncAppender).
See the LMAX Disruptor documentation
for more information about the advantages of using a RingBuffer over a BlockingQueue.
The behavior of the appender when the RingBuffer is full and the event cannot be published
is controlled by the appendTimeout configuration parameter.
By default the appender drops the event immediately, and emits a warning message every
droppedWarnFrequency consecutive dropped events.
It can also be configured to wait until some space is available, with or without timeout.
A single handler thread will be used to handle the actual handling of the event.
Subclasses must implement createEventHandler() to provide a EventHandler to
define the logic that executes in the handler thread.
For example, DelegatingAsyncDisruptorAppender will delegate
appending of the event to another appender in the handler thread.
By default, child threads created by this appender will be daemon threads,
and therefore allow the JVM to exit gracefully without
needing to explicitly shut down the appender.
Note that in this case, it is possible for appended log events to not
be handled (if the child thread has not had a chance to process them yet).
By setting setDaemon(boolean) to false, you can change this behavior.
When false, child threads created by this appender will not be daemon threads,
and therefore will prevent the JVM from shutting down
until the appender is explicitly shut down.
Set this to false if you want to ensure that every log event
prior to shutdown is handled.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classEvent wrapper object used for each element of theRingBuffer.protected static classFactory for creating the initialAsyncDisruptorAppender.LogEvents to populate theRingBuffer.protected static classSets theAsyncDisruptorAppender.LogEvent.eventto the logback Event. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Stringstatic final intstatic final com.lmax.disruptor.dsl.ProducerTypestatic final intstatic final Stringstatic final com.lmax.disruptor.WaitStrategyThese listeners will be notified when certain events occur on this appender.protected static final StringFields inherited from class ch.qos.logback.core.UnsynchronizedAppenderBase
name, startedFields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(Listener listener) protected voidprotected Stringprotected abstract com.lmax.disruptor.EventHandler<AsyncDisruptorAppender.LogEvent<Event>>Create theEventHandlerto process events as they become available from the RingBuffer.protected voidprotected voidprotected voidfireEventAppended(Event event, long durationInNanos) protected voidfireEventAppendFailed(Event event, Throwable reason) protected com.lmax.disruptor.dsl.Disruptor<AsyncDisruptorAppender.LogEvent<Event>>intprotected com.lmax.disruptor.EventTranslatorOneArg<AsyncDisruptorAppender.LogEvent<Event>,Event> com.lmax.disruptor.dsl.ProducerTypeGet theProducerTypeconfigured for the Disruptor.intReturns the maximum number of events allowed in the queue.com.lmax.disruptor.WaitStrategybooleanbooleanisDaemon()protected booleanTest whether the ring buffer is empty or notprotected voidvoidremoveListener(Listener listener) protected voidsafelyFireEvent(Consumer<Listener> callback) voidsetAddDefaultStatusListener(boolean addDefaultStatusListener) voidsetAppendRetryFrequency(Duration appendRetryFrequency) voidsetAppendTimeout(Duration appendTimeout) voidsetDaemon(boolean useDaemonThread) voidsetDroppedWarnFrequency(int droppedWarnFrequency) protected voidsetEventFactory(AsyncDisruptorAppender.LogEventFactory<Event> eventFactory) protected voidsetEventTranslator(com.lmax.disruptor.EventTranslatorOneArg<AsyncDisruptorAppender.LogEvent<Event>, Event> eventTranslator) voidsetProducerType(com.lmax.disruptor.dsl.ProducerType producerType) Deprecated.ProducerType will be fixed to MULTI in future release and this method removed without any replacement.voidsetRingBufferSize(int ringBufferSize) Sets the size of theRingBuffer.voidsetShutdownGracePeriod(Duration shutdownGracePeriod) voidsetThreadFactory(ThreadFactory threadFactory) voidsetThreadNameFormat(String threadNameFormat) Pattern used by the to set the handler thread names.voidsetWaitStrategy(com.lmax.disruptor.WaitStrategy waitStrategy) voidsetWaitStrategyType(String waitStrategyType) voidstart()voidstop()Methods inherited from class ch.qos.logback.core.UnsynchronizedAppenderBase
addFilter, clearAllFilters, doAppend, getCopyOfAttachedFiltersList, getFilterChainDecision, getName, isStarted, setName, toStringMethods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContextMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
-
Field Details
-
APPENDER_NAME_FORMAT
- See Also:
-
THREAD_INDEX_FORMAT
- See Also:
-
DEFAULT_THREAD_NAME_FORMAT
- See Also:
-
DEFAULT_RING_BUFFER_SIZE
public static final int DEFAULT_RING_BUFFER_SIZE- See Also:
-
DEFAULT_PRODUCER_TYPE
public static final com.lmax.disruptor.dsl.ProducerType DEFAULT_PRODUCER_TYPE -
DEFAULT_WAIT_STRATEGY
public static final com.lmax.disruptor.WaitStrategy DEFAULT_WAIT_STRATEGY -
DEFAULT_DROPPED_WARN_FREQUENCY
public static final int DEFAULT_DROPPED_WARN_FREQUENCY- See Also:
-
listeners
These listeners will be notified when certain events occur on this appender.
-
-
Constructor Details
-
AsyncDisruptorAppender
public AsyncDisruptorAppender()
-
-
Method Details
-
start
public void start()- Specified by:
startin interfaceLifeCycle- Overrides:
startin classUnsynchronizedAppenderBase<Event extends DeferredProcessingAware>
-
stop
public void stop()- Specified by:
stopin interfaceLifeCycle- Overrides:
stopin classUnsynchronizedAppenderBase<Event extends DeferredProcessingAware>
-
createEventHandler
protected abstract com.lmax.disruptor.EventHandler<AsyncDisruptorAppender.LogEvent<Event>> createEventHandler()Create theEventHandlerto process events as they become available from the RingBuffer. This method is invoked when the appender is started bystart()and a newDisruptoris initialized.- Returns:
- a
EventHandlerinstance.
-
isRingBufferEmpty
protected boolean isRingBufferEmpty()Test whether the ring buffer is empty or not- Returns:
trueif the ring buffer is empty,falseotherwise
-
append
- Specified by:
appendin classUnsynchronizedAppenderBase<Event extends DeferredProcessingAware>
-
prepareForDeferredProcessing
-
calculateThreadName
-
getThreadNameFormatParams
-
fireAppenderStarted
protected void fireAppenderStarted() -
fireAppenderStopped
protected void fireAppenderStopped() -
fireEventAppended
-
fireEventAppendFailed
-
safelyFireEvent
-
setEventFactory
-
getEventTranslator
protected com.lmax.disruptor.EventTranslatorOneArg<AsyncDisruptorAppender.LogEvent<Event>,Event> getEventTranslator() -
setEventTranslator
protected void setEventTranslator(com.lmax.disruptor.EventTranslatorOneArg<AsyncDisruptorAppender.LogEvent<Event>, Event> eventTranslator) -
getDisruptor
-
getThreadNameFormat
-
setThreadNameFormat
Pattern used by the to set the handler thread names. Defaults to "logback-appender-%1$s-%2$d".If you change the
threadFactory, then this value may not be honored.The string is a format pattern understood by
Formatter.format(String, Object...).Formatter.format(String, Object...)is used to construct the actual thread name prefix. The first argument (%1$s) is the string appender name. The second argument (%2$d) is the numerical thread index. Other arguments can be made available by subclasses.- Parameters:
threadNameFormat- the thread name format pattern
-
getRingBufferSize
public int getRingBufferSize()Returns the maximum number of events allowed in the queue.- Returns:
- the size of the ring buffer
-
setRingBufferSize
public void setRingBufferSize(int ringBufferSize) Sets the size of theRingBuffer. Must be a positive power of 2. Defaults to 8192.If the handler thread is not as fast as the producing threads, then the
RingBufferwill eventually fill up, at which point events will be dropped or the producing threads are blocked depending onappendTimeout.- Parameters:
ringBufferSize- the maximum number of entries in the queue.
-
getProducerType
public com.lmax.disruptor.dsl.ProducerType getProducerType()Get theProducerTypeconfigured for the Disruptor.- Returns:
- the
ProducerType.
-
setProducerType
Deprecated.ProducerType will be fixed to MULTI in future release and this method removed without any replacement.TheProducerTypeto use to configure the Disruptor. By default this isProducerType.MULTI. Can be set toProducerType.SINGLEfor increase performance if (and only if) only one thread will ever be appending to this appender.WARNING: unexpected behavior may occur if this parameter is set to
ProducerType.SINGLEand multiple threads are appending to this appender.- Parameters:
producerType- the type of producer
-
getWaitStrategy
public com.lmax.disruptor.WaitStrategy getWaitStrategy() -
setWaitStrategy
public void setWaitStrategy(com.lmax.disruptor.WaitStrategy waitStrategy) -
setWaitStrategyType
-
getAppendRetryFrequency
-
setAppendRetryFrequency
-
getAppendTimeout
-
setAppendTimeout
-
setShutdownGracePeriod
-
getShutdownGracePeriod
-
getThreadFactory
-
setThreadFactory
-
getDroppedWarnFrequency
public int getDroppedWarnFrequency() -
setDroppedWarnFrequency
public void setDroppedWarnFrequency(int droppedWarnFrequency) -
isDaemon
public boolean isDaemon() -
setDaemon
public void setDaemon(boolean useDaemonThread) -
addListener
-
removeListener
-
isAddDefaultStatusListener
public boolean isAddDefaultStatusListener() -
setAddDefaultStatusListener
public void setAddDefaultStatusListener(boolean addDefaultStatusListener)
-