Browse Source

切面不捕获异常

dev_shibei_match
jianjun 4 years ago
parent
commit
b5874ae175
  1. 56
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java

56
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; private DistributedLock distributedLock;
@Around("@annotation(noRepeatSubmit)") @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 { try {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); long leaseTime = noRepeatSubmit.leaseTime();
assert attributes != null; //如果获取不到锁等待0秒直接返回 持锁时间为leaseTime
HttpServletRequest request = attributes.getRequest(); lock = distributedLock.getLock(RedisKeys.getNoRepeatSubmitKey(key), leaseTime, NumConstant.ZERO_L, TimeUnit.MILLISECONDS);
String internalToken = request.getHeader(AUTHORIZATION_TOKEN_HEADER_KEY); } catch (Exception e) {
String key = getKey(request.getRequestURI(), internalToken); log.warn("noRepeatSubmit key:{},msg:{}", key, e.getMessage());
// 如果缓存中有这个url视为重复提交 //"未获取到锁,重复提交了
Object result = null; throw new RenException(EpmetErrorCode.REPEAT_SUBMIT.getCode());
RLock lock = null; }
try { try {
long leaseTime = noRepeatSubmit.leaseTime(); //因为getLock如果获取失败抛异常 所以不做锁状态的判断
//如果获取不到锁等待0秒直接返回 持锁时间为leaseTime result = pjp.proceed();
lock = distributedLock.getLock(RedisKeys.getNoRepeatSubmitKey(key), leaseTime, NumConstant.ZERO_L, TimeUnit.MILLISECONDS); } finally {
} catch (Exception e) { distributedLock.unLock(lock);
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; return result;
} catch (RenException e) {
throw e;
} catch (Throwable e) {
log.error("验证重复提交时出现未知异常!");
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode());
}
} }
private String getKey(String keyExpress, String token) { private String getKey(String keyExpress, String token) {

Loading…
Cancel
Save