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) {