Browse Source

防重复提交 token为空则直接跳过

feature/teamB_zz_wgh
jianjun 3 years ago
parent
commit
48b93f729b
  1. 18
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java

18
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java

@ -1,11 +1,13 @@
package com.epmet.commons.tools.aop; package com.epmet.commons.tools.aop;
import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.constant.StrConstant;
import com.epmet.commons.tools.distributedlock.DistributedLock; import com.epmet.commons.tools.distributedlock.DistributedLock;
import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.commons.tools.redis.RedisKeys;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
@ -46,10 +48,13 @@ public class NoRepeatSubmitAop {
Object result = null; Object result = null;
RLock lock = null; RLock lock = null;
try { try {
long leaseTime = noRepeatSubmit.leaseTime(); //key 为空则直接跳过 不加锁
//如果获取不到锁等待0秒直接返回 持锁时间为leaseTime if (key != null){
lock = distributedLock.getLock(RedisKeys.getNoRepeatSubmitKey(key), leaseTime, NumConstant.ZERO_L, TimeUnit.MILLISECONDS); long leaseTime = noRepeatSubmit.leaseTime();
//因为getLock如果获取失败抛异常 所以不做锁状态的判断 //如果获取不到锁等待0秒直接返回 持锁时间为leaseTime
lock = distributedLock.getLock(RedisKeys.getNoRepeatSubmitKey(key), leaseTime, NumConstant.ZERO_L, TimeUnit.MILLISECONDS);
//因为getLock如果获取失败抛异常 所以不做锁状态的判断
}
result = pjp.proceed(); result = pjp.proceed();
} catch (Exception e) { } catch (Exception e) {
log.warn("noRepeatSubmit key:{},msg:{}", key, e.getMessage()); log.warn("noRepeatSubmit key:{},msg:{}", key, e.getMessage());
@ -64,7 +69,10 @@ public class NoRepeatSubmitAop {
} }
private String getKey(String keyExpress, String token) { private String getKey(String keyExpress, String token) {
return keyExpress + token; if (StringUtils.isBlank(token)) {
return null;
}
return keyExpress + StrConstant.UNDER_LINE + token;
} }
} }

Loading…
Cancel
Save