Class TimeoutUnitOfWorkFactory

java.lang.Object
org.axonframework.messaging.core.timeout.TimeoutUnitOfWorkFactory
All Implemented Interfaces:
UnitOfWorkFactory

public class TimeoutUnitOfWorkFactory extends Object implements UnitOfWorkFactory
A 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:
  • AxonTimeLimitedTask
  • Constructor Details

    • TimeoutUnitOfWorkFactory

      public TimeoutUnitOfWorkFactory(UnitOfWorkFactory delegate, String componentName, int timeout, int warningThreshold, int warningInterval, ScheduledExecutorService executorService)
      Creates a new TimeoutUnitOfWorkFactory decorating the given delegate, for the given componentName with the given timeout, warningThreshold, warningInterval used 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 the ScheduledExecutorService provided by the AxonTaskJanitor to ensure a single executor is used throughout.

      Parameters:
      delegate - the delegate UnitOfWorkFactory used to create the actual UnitOfWork
      componentName - the name of the component to be included in the logging
      timeout - the timeout in milliseconds
      warningThreshold - the threshold in milliseconds after which a warning is logged. Setting this to a value higher than timeout will disable warnings
      warningInterval - the interval in milliseconds between warnings
      executorService - the executor service to schedule the timeout and warnings
  • Method Details