|
|
@@ -3,6 +3,7 @@ package com.izouma.awesomeAdmin.aspect;
|
|
|
import com.izouma.awesomeAdmin.annotations.RedisLock;
|
|
|
import com.izouma.awesomeAdmin.exception.BusinessException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
@@ -18,7 +19,11 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Aspect
|
|
|
@Component
|
|
|
@@ -50,34 +55,39 @@ public class RedisLockAspect {
|
|
|
}
|
|
|
String key = redisLock.value();
|
|
|
try {
|
|
|
- key = Optional.ofNullable(parser.parseExpression(redisLock.value()).getValue(context))
|
|
|
- .map(Object::toString)
|
|
|
- .orElse("default");
|
|
|
- } catch (Exception ignored) {
|
|
|
+ key = Optional.ofNullable(parser.parseExpression(key).getValue(context))
|
|
|
+ .map(Object::toString)
|
|
|
+ .orElse(key);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("redisLock key parse error", e);
|
|
|
}
|
|
|
- log.info("enter redisLock aspect key: {}", key);
|
|
|
RLock lock = redissonClient.getLock(key);
|
|
|
if (redisLock.behavior() == RedisLock.Behavior.WAIT) {
|
|
|
+ log.info("⌛️wait lock...{}", key);
|
|
|
lock.lock(redisLock.expire(), redisLock.unit());
|
|
|
- log.info("get redisLock success");
|
|
|
+ log.info("✅lock success {}", key);
|
|
|
} else {
|
|
|
if (!lock.tryLock()) {
|
|
|
- log.info("get redisLock fail");
|
|
|
+ log.info("❌lock fail {}", key);
|
|
|
throw new BusinessException(redisLock.message());
|
|
|
}
|
|
|
- log.info("get redisLock success");
|
|
|
+ log.info("✅lock success {}", key);
|
|
|
}
|
|
|
Object res = null;
|
|
|
try {
|
|
|
res = joinPoint.proceed();
|
|
|
try {
|
|
|
lock.unlock();
|
|
|
- } catch (Exception ignored) {
|
|
|
+ log.info("♻️lock released {}", key);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("❌️lock release failed {}", key, ex);
|
|
|
}
|
|
|
} catch (Throwable e) {
|
|
|
try {
|
|
|
lock.unlock();
|
|
|
- } catch (Exception ignored) {
|
|
|
+ log.info("♻️lock released {}", key);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("❌️lock release failed {}", key, ex);
|
|
|
}
|
|
|
if (e instanceof BusinessException) {
|
|
|
throw (BusinessException) e;
|
|
|
@@ -85,5 +95,43 @@ public class RedisLockAspect {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
return res;
|
|
|
+
|
|
|
+
|
|
|
+// try {
|
|
|
+// key = Optional.ofNullable(parser.parseExpression(redisLock.value()).getValue(context))
|
|
|
+// .map(Object::toString)
|
|
|
+// .orElse("default");
|
|
|
+// } catch (Exception ignored) {
|
|
|
+// }
|
|
|
+// log.info("enter redisLock aspect key: {}", key);
|
|
|
+// RLock lock = redissonClient.getLock(key);
|
|
|
+// if (redisLock.behavior() == RedisLock.Behavior.WAIT) {
|
|
|
+// lock.lock(redisLock.expire(), redisLock.unit());
|
|
|
+// log.info("get redisLock success");
|
|
|
+// } else {
|
|
|
+// if (!lock.tryLock()) {
|
|
|
+// log.info("get redisLock fail");
|
|
|
+// throw new BusinessException(redisLock.message());
|
|
|
+// }
|
|
|
+// log.info("get redisLock success");
|
|
|
+// }
|
|
|
+// Object res = null;
|
|
|
+// try {
|
|
|
+// res = joinPoint.proceed();
|
|
|
+// try {
|
|
|
+// lock.unlock();
|
|
|
+// } catch (Exception ignored) {
|
|
|
+// }
|
|
|
+// } catch (Throwable e) {
|
|
|
+// try {
|
|
|
+// lock.unlock();
|
|
|
+// } catch (Exception ignored) {
|
|
|
+// }
|
|
|
+// if (e instanceof BusinessException) {
|
|
|
+// throw (BusinessException) e;
|
|
|
+// }
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+// return res;
|
|
|
}
|
|
|
}
|