|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.handler.advice; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.concurrent.TimeoutException; |
| 22 | +import java.util.concurrent.locks.Lock; |
| 23 | +import java.util.function.Function; |
| 24 | + |
| 25 | +import org.springframework.expression.EvaluationContext; |
| 26 | +import org.springframework.expression.Expression; |
| 27 | +import org.springframework.integration.expression.ExpressionUtils; |
| 28 | +import org.springframework.integration.expression.FunctionExpression; |
| 29 | +import org.springframework.integration.expression.ValueExpression; |
| 30 | +import org.springframework.integration.support.locks.LockRegistry; |
| 31 | +import org.springframework.integration.support.utils.IntegrationUtils; |
| 32 | +import org.springframework.lang.Nullable; |
| 33 | +import org.springframework.messaging.Message; |
| 34 | +import org.springframework.messaging.MessageChannel; |
| 35 | +import org.springframework.util.Assert; |
| 36 | + |
| 37 | +/** |
| 38 | + * The {@link AbstractRequestHandlerAdvice} to ensure exclusive access to the |
| 39 | + * {@code AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage(Message)} calls |
| 40 | + * based on the {@code lockKey} from message. |
| 41 | + * <p> |
| 42 | + * If {@code lockKey} for the message is {@code null}, the no locking around the call. |
| 43 | + * However, if {@link } |
| 44 | + * |
| 45 | + * @author Artem Bilan |
| 46 | + * |
| 47 | + * @since 6.5 |
| 48 | + */ |
| 49 | +public class LockRequestHandlerAdvice extends AbstractRequestHandlerAdvice { |
| 50 | + |
| 51 | + private final LockRegistry lockRegistry; |
| 52 | + |
| 53 | + private final Expression lockKeyExpression; |
| 54 | + |
| 55 | + @Nullable |
| 56 | + private MessageChannel discardChannel; |
| 57 | + |
| 58 | + @Nullable |
| 59 | + private Expression waitLockDurationExpression; |
| 60 | + |
| 61 | + private EvaluationContext evaluationContext; |
| 62 | + |
| 63 | + /** |
| 64 | + * Construct an advice instance based on a {@link LockRegistry} and fixed (shared) lock key. |
| 65 | + * @param lockRegistry the {@link LockRegistry} to use. |
| 66 | + * @param lockKey the static (shared) lock key for all the calls. |
| 67 | + */ |
| 68 | + public LockRequestHandlerAdvice(LockRegistry lockRegistry, Object lockKey) { |
| 69 | + this(lockRegistry, new ValueExpression<>(lockKey)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Construct an advice instance based on a {@link LockRegistry} |
| 74 | + * and SpEL expression for the lock key against request message. |
| 75 | + * @param lockRegistry the {@link LockRegistry} to use. |
| 76 | + * @param lockKeyExpression the SpEL expression to evaluate a lock key against request message. |
| 77 | + */ |
| 78 | + public LockRequestHandlerAdvice(LockRegistry lockRegistry, Expression lockKeyExpression) { |
| 79 | + Assert.notNull(lockRegistry, "'lockRegistry' must not be null"); |
| 80 | + Assert.notNull(lockKeyExpression, "'lockKeyExpression' must not be null"); |
| 81 | + this.lockRegistry = lockRegistry; |
| 82 | + this.lockKeyExpression = lockKeyExpression; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Construct an advice instance based on a {@link LockRegistry} |
| 87 | + * and function for the lock key against request message. |
| 88 | + * @param lockRegistry the {@link LockRegistry} to use. |
| 89 | + * @param lockKeyFunction the function to evaluate a lock key against request message. |
| 90 | + */ |
| 91 | + public LockRequestHandlerAdvice(LockRegistry lockRegistry, Function<Message<?>, Object> lockKeyFunction) { |
| 92 | + Assert.notNull(lockRegistry, "'lockRegistry' must not be null"); |
| 93 | + Assert.notNull(lockKeyFunction, "'lockKeyFunction' must not be null"); |
| 94 | + this.lockRegistry = lockRegistry; |
| 95 | + this.lockKeyExpression = new FunctionExpression<>(lockKeyFunction); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Optional duration for a {@link Lock#tryLock(long, TimeUnit)} API. |
| 100 | + * Otherwise, {@link Lock#lockInterruptibly()} is used. |
| 101 | + * @param waitLockDuration the duration for {@link Lock#tryLock(long, TimeUnit)}. |
| 102 | + */ |
| 103 | + public void setWaitLockDuration(Duration waitLockDuration) { |
| 104 | + setWaitLockDurationExpression(new ValueExpression<>(waitLockDuration)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * The SpEL expression to evaluate a {@link Lock#tryLock(long, TimeUnit)} duration |
| 109 | + * against request message. |
| 110 | + * Can be evaluated to {@link Duration}, {@code long} (with meaning as milliseconds), |
| 111 | + * or to string in the duration ISO-8601 format. |
| 112 | + * @param waitLockDurationExpression SpEL expression for duration. |
| 113 | + */ |
| 114 | + public void setWaitLockDurationExpression(Expression waitLockDurationExpression) { |
| 115 | + this.waitLockDurationExpression = waitLockDurationExpression; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * The SpEL expression to evaluate a {@link Lock#tryLock(long, TimeUnit)} duration |
| 120 | + * against request message. |
| 121 | + * Can be evaluated to {@link Duration}, {@code long} (with meaning as milliseconds), |
| 122 | + * or to string in the duration ISO-8601 format. |
| 123 | + * @param waitLockDurationExpression SpEL expression for duration. |
| 124 | + */ |
| 125 | + public void setWaitLockDurationExpressionString(String waitLockDurationExpression) { |
| 126 | + this.waitLockDurationExpression = EXPRESSION_PARSER.parseExpression(waitLockDurationExpression); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * The function to evaluate a {@link Lock#tryLock(long, TimeUnit)} duration |
| 131 | + * against request message. |
| 132 | + * @param waitLockDurationFunction the function for duration. |
| 133 | + */ |
| 134 | + public void setWaitLockDurationFunction(Function<Message<?>, Duration> waitLockDurationFunction) { |
| 135 | + this.waitLockDurationExpression = new FunctionExpression<>(waitLockDurationFunction); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Set a channel where to send a message for which {@code lockKey} is evaluated to {@code null}. |
| 140 | + * If this is not set and {@code lockKey == null}, no locking around the call. |
| 141 | + * @param discardChannel the channel to send messages without a key. |
| 142 | + */ |
| 143 | + public void setDiscardChannel(@Nullable MessageChannel discardChannel) { |
| 144 | + this.discardChannel = discardChannel; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected void onInit() { |
| 149 | + super.onInit(); |
| 150 | + this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) { |
| 155 | + Object lockKey = this.lockKeyExpression.getValue(this.evaluationContext, message); |
| 156 | + if (lockKey != null) { |
| 157 | + Duration waitLockDuration = getWaitLockDuration(message); |
| 158 | + try { |
| 159 | + if (waitLockDuration == null) { |
| 160 | + return lockRegistry.executeLocked(lockKey, callback::execute); |
| 161 | + } |
| 162 | + else { |
| 163 | + return lockRegistry.executeLocked(lockKey, waitLockDuration, callback::execute); |
| 164 | + } |
| 165 | + } |
| 166 | + catch (InterruptedException ex) { |
| 167 | + Thread.currentThread().interrupt(); |
| 168 | + throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message, |
| 169 | + () -> "The lock for message was interrupted", ex); |
| 170 | + } |
| 171 | + catch (TimeoutException ex) { |
| 172 | + throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message, |
| 173 | + () -> "Could not acquire the lock in time: " + waitLockDuration, ex); |
| 174 | + } |
| 175 | + } |
| 176 | + else { |
| 177 | + if (this.discardChannel != null) { |
| 178 | + this.discardChannel.send(message); |
| 179 | + return null; |
| 180 | + } |
| 181 | + else { |
| 182 | + return callback.execute(); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Nullable |
| 188 | + private Duration getWaitLockDuration(Message<?> message) { |
| 189 | + if (this.waitLockDurationExpression != null) { |
| 190 | + Object value = this.waitLockDurationExpression.getValue(this.evaluationContext, message); |
| 191 | + if (value != null) { |
| 192 | + if (value instanceof Duration duration) { |
| 193 | + return duration; |
| 194 | + } |
| 195 | + else if (value instanceof Long aLong) { |
| 196 | + return Duration.ofMillis(aLong); |
| 197 | + } |
| 198 | + else { |
| 199 | + return Duration.parse(value.toString()); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + return null; |
| 204 | + } |
| 205 | + |
| 206 | +} |
0 commit comments