From b5874ae175e0879db87e9e30628a0c4ba2e7311a Mon Sep 17 00:00:00 2001 From: jianjun Date: Fri, 27 Aug 2021 15:32:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=87=E9=9D=A2=E4=B8=8D=E6=8D=95=E8=8E=B7?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/tools/aop/NoRepeatSubmitAop.java | 56 +++++++++---------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java index ff41a7747c..2c391d6b81 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java @@ -35,41 +35,35 @@ public class NoRepeatSubmitAop { private DistributedLock distributedLock; @Around("@annotation(noRepeatSubmit)") - public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) { + public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit noRepeatSubmit) throws Throwable { + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + assert attributes != null; + HttpServletRequest request = attributes.getRequest(); + String internalToken = request.getHeader(AUTHORIZATION_TOKEN_HEADER_KEY); + String key = getKey(request.getRequestURI(), internalToken); + // 如果缓存中有这个url视为重复提交 + Object result = null; + RLock lock = null; try { - ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - assert attributes != null; - HttpServletRequest request = attributes.getRequest(); - String internalToken = request.getHeader(AUTHORIZATION_TOKEN_HEADER_KEY); - String key = getKey(request.getRequestURI(), internalToken); - // 如果缓存中有这个url视为重复提交 - Object result = null; - RLock lock = null; - try { - long leaseTime = noRepeatSubmit.leaseTime(); - //如果获取不到锁等待0秒直接返回 持锁时间为leaseTime - lock = distributedLock.getLock(RedisKeys.getNoRepeatSubmitKey(key), leaseTime, NumConstant.ZERO_L, TimeUnit.MILLISECONDS); - } catch (Exception e) { - log.warn("noRepeatSubmit key:{},msg:{}", key, e.getMessage()); - //"未获取到锁,重复提交了 - throw new RenException(EpmetErrorCode.REPEAT_SUBMIT.getCode()); - } - try { - //因为getLock如果获取失败抛异常 所以不做锁状态的判断 - result = pjp.proceed(); - } finally { - distributedLock.unLock(lock); - } + long leaseTime = noRepeatSubmit.leaseTime(); + //如果获取不到锁等待0秒直接返回 持锁时间为leaseTime + lock = distributedLock.getLock(RedisKeys.getNoRepeatSubmitKey(key), leaseTime, NumConstant.ZERO_L, TimeUnit.MILLISECONDS); + } catch (Exception e) { + log.warn("noRepeatSubmit key:{},msg:{}", key, e.getMessage()); + //"未获取到锁,重复提交了 + throw new RenException(EpmetErrorCode.REPEAT_SUBMIT.getCode()); + } + try { + //因为getLock如果获取失败抛异常 所以不做锁状态的判断 + result = pjp.proceed(); + } finally { + distributedLock.unLock(lock); + } - return result; - } catch (RenException e) { - throw e; - } catch (Throwable e) { - log.error("验证重复提交时出现未知异常!"); - throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); - } + return result; + } private String getKey(String keyExpress, String token) {