Class AbstractDeadlineManager
- All Implemented Interfaces:
DeadlineManager
- Direct Known Subclasses:
DbSchedulerDeadlineManager,JobRunrDeadlineManager,QuartzDeadlineManager,SimpleDeadlineManager
DeadlineManager to be implemented by concrete solutions for the
DeadlineManager. Provides functionality to perform a call to the DeadlineManager when the ProcessingContext
of the current invocation prepares its commit. This runOnPrepareCommitOrNow(Consumer) functionality is
required, as the DeadlineManager schedules a Message which needs to happen in order with the other messages published
throughout the system.
Axon Framework 4 found that moment through the ambient unit of work. Axon Framework 5 has none, so a call is deferred
when it is made while a ContextAwareScope is current, which is the case while a Saga handler method runs, or
when it is made through a DeadlineManager handler parameter, which is bound to the ProcessingContext
of the handler. A call made anywhere else runs immediately.
- Since:
- 3.3
- Author:
- Steven van Beelen
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate anAbstractDeadlineManager, registering theLegacySupportAxoniqAddonwith the license entitlement system. -
Method Summary
Modifier and TypeMethodDescriptionprotected DeadlineMessageasDeadlineMessage(String deadlineName, @Nullable Object messageOrPayload, Instant expiryTime) Returns the givendeadlineNameandmessageOrPayloadas a DeadlineMessage which expires at the givenexpiryTime.protected List<MessageDispatchInterceptor<? super DeadlineMessage>> Provides a list of registered dispatch interceptors.protected List<MessageHandlerInterceptor<? super DeadlineMessage>> Provides a list of registered handler interceptors.protected DeadlineMessageprocessDispatchInterceptors(DeadlineMessage message, @Nullable ProcessingContext context) Applies registeredMessageDispatchInterceptors to the givenmessage, handing them the givencontext.registerDispatchInterceptor(MessageDispatchInterceptor<? super DeadlineMessage> dispatchInterceptor) Registers the givendispatchInterceptor, which is applied to everyDeadlineMessagebefore it is scheduled.registerHandlerInterceptor(MessageHandlerInterceptor<? super DeadlineMessage> handlerInterceptor) Registers the givenhandlerInterceptor, which is applied around the handling of everyDeadlineMessagewhen its deadline fires.protected voidrunOnPrepareCommitOrNow(Consumer<@Nullable ProcessingContext> deadlineCall) Run a givendeadlineCallimmediately, or defer it to a phase of aProcessingContext.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.axonframework.deadline.DeadlineManager
cancelAll, cancelAllWithinScope, cancelAllWithinScope, cancelSchedule, schedule, schedule, schedule, schedule, schedule, schedule, shutdown
-
Constructor Details
-
AbstractDeadlineManager
protected AbstractDeadlineManager()Instantiate anAbstractDeadlineManager, registering theLegacySupportAxoniqAddonwith the license entitlement system.
-
-
Method Details
-
runOnPrepareCommitOrNow
Run a givendeadlineCallimmediately, or defer it to a phase of aProcessingContext. A call made through acontext-bound viewof this manager is deferred to the context the view was made for. Any other call is deferred to the context carried by the currentContextAwareScope, if one is active. That phase runs after the Saga write and before the context commits. This is required as the DeadlineManager schedules messages which we want to happen in order with other messages being handled.The view's context wins over the scope's, as it is the innermost one: a command a Saga handler sends can run on the Saga's thread, in a context of its own, while the Saga's scope is still current. Axon Framework 4 deferred such a call to the command's unit of work, so a failing command scheduled nothing even when the Saga survived its failure. Within the Saga handler itself, the view and the scope carry the same context.
Deferred calls run in the order they were made, also across Sagas sharing the same
ProcessingContext, as they did in the prepare-commit phase of an Axon Framework 4 unit of work. They never run when the context rolls back before reaching that phase.The call is handed the
ProcessingContextit was deferred to, ornullwhen it runs immediately. Concrete deadline managers pass it on toprocessDispatchInterceptors(DeadlineMessage, ProcessingContext), so that the dispatch interceptors of a deferred deadline see the context it was deferred to. The call cannot look that context up itself: by the time a deferred call runs, the scope or the view that provided it is no longer current.- Parameters:
deadlineCall- the call to be executed now, or when theProcessingContextit is deferred to prepares its commit, receiving that context ornull- Throws:
IllegalStateException- if the calls deferred to thatProcessingContextalready ran, as nothing would run the givendeadlineCallanymore
-
registerDispatchInterceptor
public Registration registerDispatchInterceptor(MessageDispatchInterceptor<? super DeadlineMessage> dispatchInterceptor) Registers the givendispatchInterceptor, which is applied to everyDeadlineMessagebefore it is scheduled.Concrete deadline managers apply the registered interceptors through
processDispatchInterceptors(DeadlineMessage, ProcessingContext), inside the deferred call when the call is deferred.- Parameters:
dispatchInterceptor- the interceptor to apply to deadline messages before they are scheduled- Returns:
- a
Registrationthat removes the givendispatchInterceptoragain when cancelled
-
registerHandlerInterceptor
public Registration registerHandlerInterceptor(MessageHandlerInterceptor<? super DeadlineMessage> handlerInterceptor) Registers the givenhandlerInterceptor, which is applied around the handling of everyDeadlineMessagewhen its deadline fires.Concrete deadline managers apply the registered interceptors, obtained through
handlerInterceptors(), when they deliver a fired deadline to its scope.- Parameters:
handlerInterceptor- the interceptor to apply around the handling of fired deadline messages- Returns:
- a
Registrationthat removes the givenhandlerInterceptoragain when cancelled
-
dispatchInterceptors
Provides a list of registered dispatch interceptors. Do note that this list is not modifiable, and that changes in the internal structure for dispatch interceptors will be reflected in this list.- Returns:
- a list of dispatch interceptors
-
handlerInterceptors
Provides a list of registered handler interceptors. Do note that this list is not modifiable, and that changes in the internal structure for handler interceptors will be reflected in this list.- Returns:
- a list of handler interceptors
-
processDispatchInterceptors
protected DeadlineMessage processDispatchInterceptors(DeadlineMessage message, @Nullable ProcessingContext context) Applies registeredMessageDispatchInterceptors to the givenmessage, handing them the givencontext.Before the interceptors run, the correlation data the
contextcarries, as put there by aCorrelationDataInterceptoraround the handler that made the deadline call, is added to the message's metadata. Metadata the message already holds under the same key is kept. A deadline thereby carries the correlation data of the message whose handling scheduled it, with the precedence of Axon Framework 4, which created a message with the correlation data of the current unit of work beneath its own metadata. This deliberately differs from a message dispatched through a bus, on which theCorrelationDataInterceptorlets the correlation data replace the message's own metadata under the same key.The
contextis the one of the scope a deadline call was made in, as handed out byrunOnPrepareCommitOrNow(Consumer), ornullfor a call that runs immediately, outside anyProcessingContext. A failing interceptor fails this call with the interceptor's own exception, and an interceptor that ends the chain without a message fails it with anIllegalStateException, instead of scheduling anullmessage.- Parameters:
message- the deadline message to be interceptedcontext- the context the deadline call runs for, ornullif it runs outside one- Returns:
- the intercepted message, never
null - Throws:
IllegalStateException- if an interceptor ended the chain without a message
-
asDeadlineMessage
protected DeadlineMessage asDeadlineMessage(String deadlineName, @Nullable Object messageOrPayload, Instant expiryTime) Returns the givendeadlineNameandmessageOrPayloadas a DeadlineMessage which expires at the givenexpiryTime. If themessageOrPayloadparameter is of typeMessage, a newDeadlineMessageinstance will be created using the payload and metadata of the given message. Otherwise, the givenmessageOrPayloadis wrapped into aGenericDeadlineMessageas its payload.- Parameters:
deadlineName- the name for thisDeadlineMessagemessageOrPayload- aMessageor payload to wrap as a DeadlineMessageexpiryTime- the timestamp at which the deadline expires- Returns:
- a DeadlineMessage using the
deadlineNameas its deadline name and containing the givenmessageOrPayloadas the payload
-