package com.qianwen.core.redis.lock;
|
|
import java.lang.reflect.Method;
|
import java.util.concurrent.TimeUnit;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import com.qianwen.core.tool.spel.BladeExpressionEvaluator;
|
import com.qianwen.core.tool.utils.StringUtil;
|
import org.springframework.beans.BeansException;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.expression.AnnotatedElementKey;
|
import org.springframework.expression.EvaluationContext;
|
import org.springframework.util.Assert;
|
|
@Aspect
|
/* loaded from: blade-starter-redis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/redis/lock/RedisLockAspect.class */
|
public class RedisLockAspect implements ApplicationContextAware {
|
private static final BladeExpressionEvaluator EVALUATOR = new BladeExpressionEvaluator();
|
private final RedisLockClient redisLockClient;
|
private ApplicationContext applicationContext;
|
|
public RedisLockAspect(final RedisLockClient redisLockClient) {
|
this.redisLockClient = redisLockClient;
|
}
|
|
@Around("@annotation(redisLock)")
|
public Object aroundRedisLock(ProceedingJoinPoint point, RedisLock redisLock) {
|
String lockKey;
|
String lockName = redisLock.value();
|
Assert.hasText(lockName, "@RedisLock value must have length; it must not be null or empty");
|
String lockParam = redisLock.param();
|
if (StringUtil.isNotBlank(lockParam)) {
|
String evalAsText = evalLockParam(point, lockParam);
|
lockKey = lockName + ':' + evalAsText;
|
} else {
|
lockKey = lockName;
|
}
|
LockType lockType = redisLock.type();
|
long waitTime = redisLock.waitTime();
|
long leaseTime = redisLock.leaseTime();
|
TimeUnit timeUnit = redisLock.timeUnit();
|
|
return this.redisLockClient.lock(lockKey, lockType, waitTime, leaseTime, timeUnit, point::proceed);
|
}
|
|
private String evalLockParam(ProceedingJoinPoint point, String lockParam) {
|
MethodSignature ms = (MethodSignature)point.getSignature();
|
//MethodSignature ms = point.getSignature();
|
Method method = ms.getMethod();
|
Object[] args = point.getArgs();
|
Object target = point.getTarget();
|
Class<?> targetClass = target.getClass();
|
EvaluationContext context = EVALUATOR.createContext(method, args, target, targetClass, this.applicationContext);
|
AnnotatedElementKey elementKey = new AnnotatedElementKey(method, targetClass);
|
return EVALUATOR.evalAsText(lockParam, elementKey, context);
|
}
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
this.applicationContext = applicationContext;
|
}
|
}
|