From 56feff0455f33e8ef7b4d0429792a82413fcc98b Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期四, 03 三月 2022 22:05:31 +0800
Subject: [PATCH] update 优化 RepeatSubmit 注解 支持业务处理失败 与 异常快速放行
---
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java | 37 ++++++++++++++++++++++++++++++++++++-
1 files changed, 36 insertions(+), 1 deletions(-)
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
index e7bbbdd..f519370 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
@@ -5,6 +5,7 @@
import cn.hutool.crypto.SecureUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.ServletUtils;
@@ -14,6 +15,8 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.AfterReturning;
+import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@@ -27,7 +30,7 @@
import java.util.concurrent.TimeUnit;
/**
- * 闃叉閲嶅鎻愪氦
+ * 闃叉閲嶅鎻愪氦(鍙傝�冪編鍥TIS闃查噸绯荤粺)
*
* @author Lion Li
*/
@@ -36,6 +39,8 @@
@Aspect
@Component
public class RepeatSubmitAspect {
+
+ private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
private final RepeatSubmitProperties repeatSubmitProperties;
@@ -64,12 +69,42 @@
String key = RedisUtils.getCacheObject(cacheRepeatKey);
if (key == null) {
RedisUtils.setCacheObject(cacheRepeatKey, "", interval, TimeUnit.MILLISECONDS);
+ KEY_CACHE.set(cacheRepeatKey);
} else {
throw new ServiceException(repeatSubmit.message());
}
}
/**
+ * 澶勭悊瀹岃姹傚悗鎵ц
+ *
+ * @param joinPoint 鍒囩偣
+ */
+ @AfterReturning(pointcut = "@annotation(repeatSubmit)", returning = "jsonResult")
+ public void doAfterReturning(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Object jsonResult) {
+ if (jsonResult instanceof R) {
+ R<?> r = (R<?>) jsonResult;
+ if (r.getCode() == R.SUCCESS) {
+ return;
+ }
+ RedisUtils.deleteObject(KEY_CACHE.get());
+ KEY_CACHE.remove();
+ }
+ }
+
+ /**
+ * 鎷︽埅寮傚父鎿嶄綔
+ *
+ * @param joinPoint 鍒囩偣
+ * @param e 寮傚父
+ */
+ @AfterThrowing(value = "@annotation(repeatSubmit)", throwing = "e")
+ public void doAfterThrowing(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Exception e) {
+ RedisUtils.deleteObject(KEY_CACHE.get());
+ KEY_CACHE.remove();
+ }
+
+ /**
* 鍙傛暟鎷艰
*/
private String argsArrayToString(Object[] paramsArray) {
--
Gitblit v1.9.3