Class LockRequestHandlerAdvice

All Implemented Interfaces:
Advice, Interceptor, MethodInterceptor, Aware, BeanFactoryAware, BeanNameAware, InitializingBean, ApplicationContextAware, ComponentSourceAware, ExpressionCapable, NamedComponent

public class LockRequestHandlerAdvice extends AbstractRequestHandlerAdvice
The AbstractRequestHandlerAdvice to ensure exclusive access to the AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage(Message) calls based on the lockKey from message.

If lockKey for the message is null, the no locking around the call. However, if setDiscardChannel(MessageChannel) is provided, such a message will be sent there instead.

Since:
6.5
Author:
Artem Bilan
  • Constructor Details

    • LockRequestHandlerAdvice

      public LockRequestHandlerAdvice(LockRegistry lockRegistry, Object lockKey)
      Construct an advice instance based on a LockRegistry and fixed (shared) lock key.
      Parameters:
      lockRegistry - the LockRegistry to use.
      lockKey - the static (shared) lock key for all the calls.
    • LockRequestHandlerAdvice

      public LockRequestHandlerAdvice(LockRegistry lockRegistry, Expression lockKeyExpression)
      Construct an advice instance based on a LockRegistry and SpEL expression for the lock key against request message.
      Parameters:
      lockRegistry - the LockRegistry to use.
      lockKeyExpression - the SpEL expression to evaluate a lock key against request message.
    • LockRequestHandlerAdvice

      public LockRequestHandlerAdvice(LockRegistry lockRegistry, Function<Message<?>,Object> lockKeyFunction)
      Construct an advice instance based on a LockRegistry and function for the lock key against request message.
      Parameters:
      lockRegistry - the LockRegistry to use.
      lockKeyFunction - the function to evaluate a lock key against request message.
  • Method Details

    • setWaitLockDuration

      public void setWaitLockDuration(Duration waitLockDuration)
      Optional duration for a Lock.tryLock(long, TimeUnit) API. Otherwise, Lock.lockInterruptibly() is used.
      Parameters:
      waitLockDuration - the duration for Lock.tryLock(long, TimeUnit).
    • setWaitLockDurationExpression

      public void setWaitLockDurationExpression(Expression waitLockDurationExpression)
      The SpEL expression to evaluate a Lock.tryLock(long, TimeUnit) duration against request message. Can be evaluated to Duration, long (with meaning as milliseconds), or to string in the duration ISO-8601 format.
      Parameters:
      waitLockDurationExpression - SpEL expression for duration.
    • setWaitLockDurationExpressionString

      public void setWaitLockDurationExpressionString(String waitLockDurationExpression)
      The SpEL expression to evaluate a Lock.tryLock(long, TimeUnit) duration against request message. Can be evaluated to Duration, long (with meaning as milliseconds), or to string in the duration ISO-8601 format.
      Parameters:
      waitLockDurationExpression - SpEL expression for duration.
    • setWaitLockDurationFunction

      public void setWaitLockDurationFunction(Function<Message<?>,Duration> waitLockDurationFunction)
      The function to evaluate a Lock.tryLock(long, TimeUnit) duration against request message.
      Parameters:
      waitLockDurationFunction - the function for duration.
    • setDiscardChannel

      public void setDiscardChannel(@Nullable MessageChannel discardChannel)
      Set a channel where to send a message for which lockKey is evaluated to null. If this is not set and lockKey == null, no locking around the call.
      Parameters:
      discardChannel - the channel to send messages without a key.
    • onInit

      protected void onInit()
      Description copied from class: IntegrationObjectSupport
      Subclasses may implement this for initialization logic.
      Overrides:
      onInit in class IntegrationObjectSupport
    • doInvoke

      protected Object doInvoke(AbstractRequestHandlerAdvice.ExecutionCallback callback, Object target, Message<?> message)
      Description copied from class: AbstractRequestHandlerAdvice
      Subclasses implement this method to apply behavior to the MessageHandler.

      callback.execute() invokes the handler method and returns its result, or null.

      Specified by:
      doInvoke in class AbstractRequestHandlerAdvice
      Parameters:
      callback - Subclasses invoke the execute() method on this interface to invoke the handler method.
      target - The target handler.
      message - The message that will be sent to the handler.
      Returns:
      the result after invoking the MessageHandler.