Class AsyncDisruptorAppender<Event extends DeferredProcessingAware,Listener extends AppenderListener<Event>>
- Type Parameters:
Event
- type of event (ILoggingEvent
orIAccessEvent
).
- 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 class
Event wrapper object used for each element of theRingBuffer
.protected static class
Factory for creating the initialAsyncDisruptorAppender.LogEvent
s to populate theRingBuffer
.protected static class
Sets theAsyncDisruptorAppender.LogEvent.event
to the logback Event. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final String
static final int
static final com.lmax.disruptor.dsl.ProducerType
static final int
static final String
static final com.lmax.disruptor.WaitStrategy
These listeners will be notified when certain events occur on this appender.protected static final String
Fields inherited from class ch.qos.logback.core.UnsynchronizedAppenderBase
name, started
Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(Listener listener) protected void
protected String
protected abstract com.lmax.disruptor.EventHandler<AsyncDisruptorAppender.LogEvent<Event>>
Create theEventHandler
to process events as they become available from the RingBuffer.protected void
protected void
protected void
fireEventAppended
(Event event, long durationInNanos) protected void
fireEventAppendFailed
(Event event, Throwable reason) protected com.lmax.disruptor.dsl.Disruptor<AsyncDisruptorAppender.LogEvent<Event>>
int
protected com.lmax.disruptor.EventTranslatorOneArg<AsyncDisruptorAppender.LogEvent<Event>,
Event> com.lmax.disruptor.dsl.ProducerType
Get theProducerType
configured for the Disruptor.int
Returns the maximum number of events allowed in the queue.com.lmax.disruptor.WaitStrategy
boolean
boolean
isDaemon()
protected boolean
Test whether the ring buffer is empty or notprotected void
void
removeListener
(Listener listener) protected void
safelyFireEvent
(Consumer<Listener> callback) void
setAddDefaultStatusListener
(boolean addDefaultStatusListener) void
setAppendRetryFrequency
(Duration appendRetryFrequency) void
setAppendTimeout
(Duration appendTimeout) void
setDaemon
(boolean useDaemonThread) void
setDroppedWarnFrequency
(int droppedWarnFrequency) protected void
setEventFactory
(AsyncDisruptorAppender.LogEventFactory<Event> eventFactory) protected void
setEventTranslator
(com.lmax.disruptor.EventTranslatorOneArg<AsyncDisruptorAppender.LogEvent<Event>, Event> eventTranslator) void
setProducerType
(com.lmax.disruptor.dsl.ProducerType producerType) Deprecated.ProducerType will be fixed to MULTI in future release and this method removed without any replacement.void
setRingBufferSize
(int ringBufferSize) Sets the size of theRingBuffer
.void
setShutdownGracePeriod
(Duration shutdownGracePeriod) void
setThreadFactory
(ThreadFactory threadFactory) void
setThreadNameFormat
(String threadNameFormat) Pattern used by the to set the handler thread names.void
setWaitStrategy
(com.lmax.disruptor.WaitStrategy waitStrategy) void
setWaitStrategyType
(String waitStrategyType) void
start()
void
stop()
Methods inherited from class ch.qos.logback.core.UnsynchronizedAppenderBase
addFilter, clearAllFilters, doAppend, getCopyOfAttachedFiltersList, getFilterChainDecision, getName, isStarted, setName, toString
Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContext
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods 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:
start
in interfaceLifeCycle
- Overrides:
start
in classUnsynchronizedAppenderBase<Event extends DeferredProcessingAware>
-
stop
public void stop()- Specified by:
stop
in interfaceLifeCycle
- Overrides:
stop
in classUnsynchronizedAppenderBase<Event extends DeferredProcessingAware>
-
createEventHandler
protected abstract com.lmax.disruptor.EventHandler<AsyncDisruptorAppender.LogEvent<Event>> createEventHandler()Create theEventHandler
to process events as they become available from the RingBuffer. This method is invoked when the appender is started bystart()
and a newDisruptor
is initialized.- Returns:
- a
EventHandler
instance.
-
isRingBufferEmpty
protected boolean isRingBufferEmpty()Test whether the ring buffer is empty or not- Returns:
true
if the ring buffer is empty,false
otherwise
-
append
- Specified by:
append
in 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
RingBuffer
will 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 theProducerType
configured for the Disruptor.- Returns:
- the
ProducerType
.
-
setProducerType
Deprecated.ProducerType will be fixed to MULTI in future release and this method removed without any replacement.TheProducerType
to use to configure the Disruptor. By default this isProducerType.MULTI
. Can be set toProducerType.SINGLE
for 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.SINGLE
and 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)
-