Class UnitOfWorkTimeoutInterceptor

java.lang.Object
org.axonframework.messaging.timeout.UnitOfWorkTimeoutInterceptor
All Implemented Interfaces:
MessageHandlerInterceptor<Message<?>>

public class UnitOfWorkTimeoutInterceptor extends Object implements MessageHandlerInterceptor<Message<?>>
Message handler interceptor that sets a timeout on the processing of the current UnitOfWork. If the timeout is reached, the thread is interrupted and the transaction will be rolled back automatically.

Note: Due to interceptor ordering, this interceptor may not be the first in the chain. We are unable to work around this, and as such the timeout measuring starts from the moment this interceptor is invoked, and ends measuring when the commit of the UnitOfWork is completed.

Since:
4.11.0
Author:
Mitchell Herrijgers
  • Constructor Details

    • UnitOfWorkTimeoutInterceptor

      public UnitOfWorkTimeoutInterceptor(String componentName, int timeout, int warningThreshold, int warningInterval)
      Creates a new UnitOfWorkTimeoutInterceptor for the given componentName with the given timeout, warningThreshold and warningInterval. The warnings and timeout will be scheduled on the AxonTaskJanitor.INSTANCE. If you want to use a different ScheduledExecutorService or Logger to log on, use the other UnitOfWorkTimeoutInterceptor(String, int, int, int, ScheduledExecutorService, Logger).
      Parameters:
      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.
    • UnitOfWorkTimeoutInterceptor

      public UnitOfWorkTimeoutInterceptor(String componentName, int timeout, int warningThreshold, int warningInterval, ScheduledExecutorService executorService, org.slf4j.Logger logger)
      Creates a new UnitOfWorkTimeoutInterceptor for the given componentName with the given timeout, warningThreshold and warningInterval. The warnings and timeout will be scheduled on the provided executorService.
      Parameters:
      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
      logger - The logger to log warnings and errors
  • Method Details

    • handle

      public Object handle(@Nonnull UnitOfWork<? extends Message<?>> unitOfWork, @Nonnull InterceptorChain interceptorChain) throws Exception
      Description copied from interface: MessageHandlerInterceptor
      Invoked before a Message is handled by a designated MessageHandler.

      The interceptor is responsible for the continuation of the handling process by invoking the InterceptorChain.proceed() method on the given interceptorChain.

      The given unitOfWork contains contextual information. Any information gathered by interceptors may be attached to the unitOfWork.

      Interceptors are highly recommended not to change the type of the message handling result, as the dispatching component might expect a result of a specific type.

      Specified by:
      handle in interface MessageHandlerInterceptor<Message<?>>
      Parameters:
      unitOfWork - The UnitOfWork that is processing the message
      interceptorChain - The interceptor chain that allows this interceptor to proceed the dispatch process
      Returns:
      the result of the message handler. May have been modified by interceptors.
      Throws:
      Exception - any exception that occurs while handling the message