Class TimeoutUnitOfWorkFactory
- All Implemented Interfaces:
UnitOfWorkFactory
UnitOfWorkFactory that decorates another factory, setting a timeout on every UnitOfWork it
creates.
If the timeout is reached, every thread currently processing the UnitOfWork is interrupted. A
UnitOfWork may run more than one phase action for the same phase concurrently, each on a different thread
(for example, dispatched through a configured Executor). The interceptor installed by this factory registers
and unregisters each such thread with the AxonTimeLimitedTask as its phase actions start and complete, so a
fired timeout reaches every thread genuinely still executing guarded work, not only the most recently started one.
The timeout starts counting from the moment the first phase action of the created UnitOfWork begins
executing, not from the moment create(String, Function) itself is called, so time spent between creation and
the actual start of processing is not counted against it. Since every UnitOfWork is created exactly once, a
single AxonTimeLimitedTask is used per UnitOfWork, started lazily on whichever phase action runs
first for that instance, and completed in a dedicated phase ordered after every other phase, so it keeps guarding the
UnitOfWork for the full duration of its AFTER_COMMIT phase actions.
Detecting a fired timeout whose interruption was swallowed by a phase action (for example, an event handler using the
default LoggingErrorHandler) is handled automatically: this factory installs a
ProcessingLifecycleInterceptor on every UnitOfWork it creates, via
UnitOfWorkConfiguration.addLifecycleInterceptor(ProcessingLifecycleInterceptor), wrapping every phase action
so a swallowed interruption is converted into an AxonTimeoutException. Since a failed phase halts all later
phases, this uniform wrapping cannot doubly convert the same fired timeout, and it covers every phase (not only the
invocation), including PREPARE_COMMIT, COMMIT, and AFTER_COMMIT. Callers no longer need to
check for a swallowed interruption themselves.
- Since:
- 5.4.0
- Author:
- Mitchell Herrijgers, Steven van Beelen
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTimeoutUnitOfWorkFactory(UnitOfWorkFactory delegate, String componentName, int timeout, int warningThreshold, int warningInterval, ScheduledExecutorService executorService) Creates a newTimeoutUnitOfWorkFactorydecorating the givendelegate, for the givencomponentNamewith the giventimeout,warningThreshold,warningIntervalused to decide when a timeout and warning should occur. -
Method Summary
Modifier and TypeMethodDescriptioncreate(String identifier, Function<UnitOfWorkConfiguration, UnitOfWorkConfiguration> customization) Creates a newUnitOfWorkwith the given identifier and applies the provided customization to its configuration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.axonframework.messaging.core.unitofwork.UnitOfWorkFactory
create, create, create
-
Constructor Details
-
TimeoutUnitOfWorkFactory
public TimeoutUnitOfWorkFactory(UnitOfWorkFactory delegate, String componentName, int timeout, int warningThreshold, int warningInterval, ScheduledExecutorService executorService) Creates a newTimeoutUnitOfWorkFactorydecorating the givendelegate, for the givencomponentNamewith the giventimeout,warningThreshold,warningIntervalused to decide when a timeout and warning should occur.The warnings and timeout will be scheduled on the provided
executorService, which is strongly recommended to be theScheduledExecutorServiceprovided by theAxonTaskJanitorto ensure a single executor is used throughout.- Parameters:
delegate- the delegateUnitOfWorkFactoryused to create the actualUnitOfWorkcomponentName- the name of the component to be included in the loggingtimeout- the timeout in millisecondswarningThreshold- the threshold in milliseconds after which a warning is logged. Setting this to a value higher thantimeoutwill disable warningswarningInterval- the interval in milliseconds between warningsexecutorService- the executor service to schedule the timeout and warnings
-
-
Method Details
-
create
public UnitOfWork create(String identifier, Function<UnitOfWorkConfiguration, UnitOfWorkConfiguration> customization) Description copied from interface:UnitOfWorkFactoryCreates a newUnitOfWorkwith the given identifier and applies the provided customization to its configuration.- Specified by:
createin interfaceUnitOfWorkFactory- Parameters:
identifier- The identifier for the unit of work.customization- A function to customize the unit of work's configuration.- Returns:
- A new
UnitOfWorkinstance.
-