Class PooledStreamingEventProcessor
- All Implemented Interfaces:
EventProcessor,StreamingEventProcessor,Lifecycle,MessageHandlerInterceptorSupport<EventMessage<?>>
StreamingEventProcessor implementation which pools its resources to enhance processing speed. It utilizes a
Coordinator as the means to stream events from a StreamableMessageSource and creates so-called work
packages. Every work package is in charge of a Segment of the entire event stream. It is the
Coordinator's job to retrieve the events from the source and provide the events to all the work packages it
is in charge of.
This approach utilizes two threads pools. One to retrieve the events to provide them to the work packages and another
to actual handle the events. Respectively, the coordinator thread pool and the work package thread pool. It is this
approach which allows for greater parallelization and processing speed than the
TrackingEventProcessor.
If no TrackingTokens are present for this processor, the PooledStreamingEventProcessor will
initialize them in a given segment count. By default, it will create 16 segments, which can be configured
through the PooledStreamingEventProcessor.Builder.initialSegmentCount(int).
- Since:
- 4.5
- Author:
- Allard Buijze, Steven van Beelen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class to instantiate aPooledStreamingEventProcessor.Nested classes/interfaces inherited from interface org.axonframework.lifecycle.Lifecycle
Lifecycle.LifecycleHandler, Lifecycle.LifecycleRegistry -
Field Summary
Fields inherited from class org.axonframework.eventhandling.AbstractEventProcessor
spanFactory -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate aPooledStreamingEventProcessorbased on the fields contained in thePooledStreamingEventProcessor.Builder. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Instantiate a Builder to be able to create aPooledStreamingEventProcessor.claimSegment(int segmentId) Instructs the processor to claim the segment with givensegmentIdand start processing it as soon as possible.Returns the unique identifier of theTokenStoreused by thisStreamingEventProcessor.booleanisError()Indicates whether the processor has been shut down due to an error.booleanIndicates whether this processor is currently running (i.e. consuming events from its message source).intSpecifies the maximum amount of segments thisEventProcessorcan process at the same time.mergeSegment(int segmentId) Instruct the processor to merge the segment with givensegmentIdback with the segment that it was originally split from.Returns the status for each of the segments processed by this processor asEventTrackerStatusinstances.voidRegisters the activities to be executed in the various phases of an application's lifecycle.voidreleaseSegment(int segmentId) Instructs the processor to release the segment with givensegmentId.voidreleaseSegment(int segmentId, long releaseDuration, TimeUnit unit) Instructs the processor to release the segment with givensegmentId.voidResets tokens to their initial state.voidresetTokens(Function<StreamableMessageSource<TrackedEventMessage<?>>, TrackingToken> initialTrackingTokenSupplier) Reset tokens to the position as return by the giveninitialTrackingTokenSupplier.<R> voidresetTokens(Function<StreamableMessageSource<TrackedEventMessage<?>>, TrackingToken> initialTrackingTokenSupplier, R resetContext) Reset tokens to the position as return by the giveninitialTrackingTokenSupplier.voidresetTokens(TrackingToken startPosition) Resets tokens to the givenstartPosition.<R> voidresetTokens(TrackingToken startPosition, R resetContext) Resets tokens to the givenstartPosition.<R> voidresetTokens(R resetContext) Resets tokens to their initial state.voidshutDown()Stops processing events.Initiates a shutdown, providing aCompletableFuturethat completes when the shutdown process is finished.splitSegment(int segmentId) Instruct the processor to split the segment with givensegmentIdinto two segments, allowing an additional process to start processing events from it.voidstart()Start processing events.booleanIndicates whether thisStreamingEventProcessorsupports a "reset".Methods inherited from class org.axonframework.eventhandling.AbstractEventProcessor
canHandle, canHandleType, eventHandlerInvoker, getHandlerInterceptors, getName, processInUnitOfWork, processInUnitOfWork, registerHandlerInterceptor, reportIgnored, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.axonframework.eventhandling.EventProcessor
getHandlerInterceptors, getNameMethods inherited from interface org.axonframework.messaging.MessageHandlerInterceptorSupport
registerHandlerInterceptorMethods inherited from interface org.axonframework.eventhandling.StreamingEventProcessor
isReplaying
-
Constructor Details
-
PooledStreamingEventProcessor
Instantiate aPooledStreamingEventProcessorbased on the fields contained in thePooledStreamingEventProcessor.Builder.Will assert the following for their presence prior to constructing this processor:
- The Event Processor's
name. - An
EventHandlerInvoker. - A
StreamableMessageSource. - A
TokenStore. - A
TransactionManager. - A
ScheduledExecutorServicefor coordination. - A
ScheduledExecutorServiceto process work packages.
AxonConfigurationExceptionis thrown.- Parameters:
builder- thePooledStreamingEventProcessor.Builderused to instantiate aPooledStreamingEventProcessorinstance
- The Event Processor's
-
-
Method Details
-
builder
Instantiate a Builder to be able to create aPooledStreamingEventProcessor.Upon initialization of this builder, the following fields are defaulted:
- The
RollbackConfigurationTypedefaults to aRollbackConfigurationType.ANY_THROWABLE. - The
ErrorHandleris defaulted to aPropagatingErrorHandler. - The
MessageMonitordefaults to aNoOpMessageMonitor. - The
initialSegmentCountdefaults to16. - The
initialTokenfunction defaults to aReplayTokenthat starts streaming from thetailwith the replay flag enabled until theheadat the moment of initialization is reached. *
- The
tokenClaimIntervaldefaults to5000milliseconds. - The
MaxSegmentProvider(used bymaxCapacity()) defaults toMaxSegmentProvider.maxShort(). - The
claimExtensionThresholddefaults to5000milliseconds. - The
batchSizedefaults to1. - The
Clockdefaults toGenericEventMessage.clock. - The
EventProcessorSpanFactorydefaults to aDefaultEventProcessorSpanFactorybacked by aNoOpSpanFactory. - The
coordinatorExtendsClaimsdefaults to afalse.
- The name of this
EventProcessor. - An
EventHandlerInvokerwhich will be given the events handled by this processor - A
StreamableMessageSourceused to retrieve events. - A
TokenStoreto store the progress of this processor in. - A
ScheduledExecutorServiceto coordinate events and segment operations. - A
ScheduledExecutorServiceto process work packages.
- Returns:
- a Builder to be able to create a
PooledStreamingEventProcessor
- The
-
registerLifecycleHandlers
Description copied from interface:LifecycleRegisters the activities to be executed in the various phases of an application's lifecycle. This could either be at startup, shutdown, or both.- Specified by:
registerLifecycleHandlersin interfaceLifecycle- Parameters:
handle- the lifecycle instance to register the handlers with- See Also:
-
start
public void start()Description copied from interface:EventProcessorStart processing events.- Specified by:
startin interfaceEventProcessor
-
shutDown
public void shutDown()Description copied from interface:EventProcessorStops processing events. Blocks until the shutdown is complete.- Specified by:
shutDownin interfaceEventProcessor
-
shutdownAsync
Description copied from interface:EventProcessorInitiates a shutdown, providing aCompletableFuturethat completes when the shutdown process is finished.- Specified by:
shutdownAsyncin interfaceEventProcessor- Returns:
- a CompletableFuture that completes when the shutdown process is finished.
-
isRunning
public boolean isRunning()Description copied from interface:EventProcessorIndicates whether this processor is currently running (i.e. consuming events from its message source).- Specified by:
isRunningin interfaceEventProcessor- Returns:
truewhen running, otherwisefalse
-
isError
public boolean isError()Description copied from interface:EventProcessorIndicates whether the processor has been shut down due to an error. In such case, the processor has forcefully shut down, as it wasn't able to automatically recover.Note that this method returns
falsewhen the processor was stopped usingEventProcessor.shutDown().- Specified by:
isErrorin interfaceEventProcessor- Returns:
truewhen paused due to an error, otherwisefalse
-
getTokenStoreIdentifier
Description copied from interface:StreamingEventProcessorReturns the unique identifier of theTokenStoreused by thisStreamingEventProcessor.- Specified by:
getTokenStoreIdentifierin interfaceStreamingEventProcessor- Returns:
- the unique identifier of the
TokenStoreused by thisStreamingEventProcessor
-
releaseSegment
public void releaseSegment(int segmentId) Description copied from interface:StreamingEventProcessorInstructs the processor to release the segment with givensegmentId.- Specified by:
releaseSegmentin interfaceStreamingEventProcessor- Parameters:
segmentId- the id of the segment to release
-
releaseSegment
Description copied from interface:StreamingEventProcessorInstructs the processor to release the segment with givensegmentId. This processor will not try to claim the given segment for the specifiedreleaseDurationin the givenunit, to ensure it is not immediately reclaimed. Note that this will override any previous release duration that existed for this segment. Providing a negative value will allow the segment to be immediately claimed.If the processor is not actively processing the segment with given
segmentId, claiming it will be ignored for the given timeframe nonetheless.- Specified by:
releaseSegmentin interfaceStreamingEventProcessor- Parameters:
segmentId- the id of the segment to be released for the specifiedreleaseDurationreleaseDuration- the amount of time to disregardsegmentIdfor processingunit- the unit of time used to express thereleaseDuration
-
claimSegment
Description copied from interface:StreamingEventProcessorInstructs the processor to claim the segment with givensegmentIdand start processing it as soon as possible.The given
segmentIdmust not be currently processed by a different processor instance, as that will have an active claim on the token. Claiming a segment that is already being processed will have no effect and returntrue.A
truereturn value indicates that the segment has been claimed and will be processed by this processor. TheStreamingEventProcessormay postpone start of work until after completion of this task, as long as the token has been claimed so work can be started. A return value offalseindicates that the segment has not been claimed due to the token for that segment not being available.- Specified by:
claimSegmentin interfaceStreamingEventProcessor- Parameters:
segmentId- the identifier of the segment to claim and start processing- Returns:
- a
CompletableFutureproviding the result of the claim operation
-
splitSegment
Description copied from interface:StreamingEventProcessorInstruct the processor to split the segment with givensegmentIdinto two segments, allowing an additional process to start processing events from it.To be able to split segments, the
TokenStoreconfigured with this processor must use explicitly initialized tokens. SeeTokenStore.requiresExplicitSegmentInitialization(). Also, the givensegmentIdmust be currently processed by a process owned by this processor instance.- Specified by:
splitSegmentin interfaceStreamingEventProcessor- Parameters:
segmentId- the identifier of the segment to split- Returns:
- a
CompletableFutureproviding the result of the split operation
-
mergeSegment
Description copied from interface:StreamingEventProcessorInstruct the processor to merge the segment with givensegmentIdback with the segment that it was originally split from. The processor must be able to claim the other segment, in order to merge it. Therefore, this other segment must not have any active claims in theTokenStore.The processor must currently be actively processing the segment with given
segmentId.Use
StreamingEventProcessor.releaseSegment(int)to force this processor to release any claims with tokens required to merge the segments.To find out which segment a given
segmentIdshould be merged with, use the following procedure:EventTrackerStatus status = processor.processingStatus().get(segmentId); if (status == null) { // this processor is not processing segmentId, and will not be able to merge } return status.getSegment().mergeableSegmentId();- Specified by:
mergeSegmentin interfaceStreamingEventProcessor- Parameters:
segmentId- the identifier of the segment to merge- Returns:
- a
CompletableFutureindicating whether the merge was executed successfully
-
supportsReset
public boolean supportsReset()Description copied from interface:StreamingEventProcessorIndicates whether thisStreamingEventProcessorsupports a "reset". Generally, a reset is supported if at least one of the Event Handling Components assigned to this processor supports it, and no handlers explicitly prevent the resets.This method should be invoked prior to invoking any of the
StreamingEventProcessor.resetTokens()operations as an early validation.- Specified by:
supportsResetin interfaceStreamingEventProcessor- Returns:
trueif resets are supported,falseotherwise
-
resetTokens
public void resetTokens()Description copied from interface:StreamingEventProcessorResets tokens to their initial state. This effectively causes a replay.Before attempting to reset the tokens, the caller must stop this processor, as well as any instances of the same logical processor that may be running in the cluster. Failure to do so will cause the reset to fail, as a processor can only reset the tokens if it is able to claim them all.
- Specified by:
resetTokensin interfaceStreamingEventProcessor
-
resetTokens
public <R> void resetTokens(R resetContext) Description copied from interface:StreamingEventProcessorResets tokens to their initial state. This effectively causes a replay. The givenresetContextwill be used to support the (optional) reset operation in an Event Handling Component.Before attempting to reset the tokens, the caller must stop this processor, as well as any instances of the same logical processor that may be running in the cluster. Failure to do so will cause the reset to fail, as a processor can only reset the tokens if it is able to claim them all.
- Specified by:
resetTokensin interfaceStreamingEventProcessor- Type Parameters:
R- the type of the providedresetContext- Parameters:
resetContext- aRused to support the reset operation
-
resetTokens
public void resetTokens(@Nonnull Function<StreamableMessageSource<TrackedEventMessage<?>>, TrackingToken> initialTrackingTokenSupplier) Description copied from interface:StreamingEventProcessorReset tokens to the position as return by the giveninitialTrackingTokenSupplier. This effectively causes a replay since that position.Note that the new token must represent a position that is before the current position of the processor.
Before attempting to reset the tokens, the caller must stop this processor, as well as any instances of the same logical processor that may be running in the cluster. Failure to do so will cause the reset to fail, as a processor can only reset the tokens if it is able to claim them all.
- Specified by:
resetTokensin interfaceStreamingEventProcessor- Parameters:
initialTrackingTokenSupplier- a function returning the token representing the position to reset to
-
resetTokens
public <R> void resetTokens(@Nonnull Function<StreamableMessageSource<TrackedEventMessage<?>>, TrackingToken> initialTrackingTokenSupplier, R resetContext) Description copied from interface:StreamingEventProcessorReset tokens to the position as return by the giveninitialTrackingTokenSupplier. This effectively causes a replay since that position. The givenresetContextwill be used to support the (optional) reset operation in an Event Handling Component.Note that the new token must represent a position that is before the current position of the processor.
Before attempting to reset the tokens, the caller must stop this processor, as well as any instances of the same logical processor that may be running in the cluster. Failure to do so will cause the reset to fail, as a processor can only reset the tokens if it is able to claim them all.
- Specified by:
resetTokensin interfaceStreamingEventProcessor- Type Parameters:
R- the type of the providedresetContext- Parameters:
initialTrackingTokenSupplier- a function returning the token representing the position to reset toresetContext- aRused to support the reset operation
-
resetTokens
Description copied from interface:StreamingEventProcessorResets tokens to the givenstartPosition. This effectively causes a replay of events since that position.Note that the new token must represent a position that is before the current position of the processor.
Before attempting to reset the tokens, the caller must stop this processor, as well as any instances of the same logical processor that may be running in the cluster. Failure to do so will cause the reset to fail, as a processor can only reset the tokens if it is able to claim them all.
- Specified by:
resetTokensin interfaceStreamingEventProcessor- Parameters:
startPosition- the token representing the position to reset the processor to
-
resetTokens
Description copied from interface:StreamingEventProcessorResets tokens to the givenstartPosition. This effectively causes a replay of events since that position. The givenresetContextwill be used to support the (optional) reset operation in an Event Handling Component.Note that the new token must represent a position that is before the current position of the processor.
Before attempting to reset the tokens, the caller must stop this processor, as well as any instances of the same logical processor that may be running in the cluster. Failure to do so will cause the reset to fail, as a processor can only reset the tokens if it is able to claim them all.
- Specified by:
resetTokensin interfaceStreamingEventProcessor- Type Parameters:
R- the type of the providedresetContext- Parameters:
startPosition- the token representing the position to reset the processor toresetContext- aRused to support the reset operation
-
maxCapacity
public int maxCapacity()Specifies the maximum amount of segments thisEventProcessorcan process at the same time.The maximum capacity of the
PooledStreamingEventProcessordefaults to 32767. If required, this value can be adjusted through thePooledStreamingEventProcessor.Builder.maxClaimedSegments(int)method.- Specified by:
maxCapacityin interfaceStreamingEventProcessor- Returns:
- the maximum amount of segments this
EventProcessorcan process at the same time
-
processingStatus
Description copied from interface:StreamingEventProcessorReturns the status for each of the segments processed by this processor asEventTrackerStatusinstances. The key of theMaprepresent the segment ids processed by this instance. The values of the returnedMaprepresent the last known status of that segment.Note that the returned
Mapis unmodifiable, but does reflect any changes made to the status as the processor is processing Events.- Specified by:
processingStatusin interfaceStreamingEventProcessor- Returns:
- the status for each of the segments processed by the current processor
-