ResubmitDataAspect.java 2.66 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
package com.lyms.talkonlineweb.aop;



import com.lyms.talkonlineweb.annotation.Resubmit;
import com.lyms.talkonlineweb.domain.BaseModel;
import com.lyms.talkonlineweb.lock.ResubmitLock;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.util.JsonUtil;
import com.lyms.talkonlineweb.util.StringUtil;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;


import java.lang.reflect.Method;

/**
* @author lqy
* 重复提交处理
*/
@Log4j2
@Aspect
@Component
public class ResubmitDataAspect {

private final static Object PRESENT = new Object();

@Around("@annotation(resubmit)")
public Object handleResubmit(ProceedingJoinPoint joinPoint, Resubmit resubmit) throws Throwable {
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
//获取注解信息
Resubmit annotation = method.getAnnotation(Resubmit.class);
int delaySeconds = annotation.delaySeconds();
Object[] pointArgs = joinPoint.getArgs();
StringBuilder key = new StringBuilder();
if (pointArgs != null && pointArgs.length > 0)
{
for (Object param : pointArgs)
{
if (param instanceof BaseModel) {
String paramStr = JsonUtil.obj2Str(param);
//生成加密参数 使用了content_MD5的加密方式
key.append(ResubmitLock.handleKey(paramStr));
}
}

if (StringUtil.isNotEmpty(key.toString()))
{
//执行锁
boolean lock = false;
try
{
//设置解锁key
lock = ResubmitLock.getInstance().lock(key.toString(), PRESENT);
if (lock)
{ //放行
return joinPoint.proceed();
}
else
{ //响应重复提交异常
BaseResponse response = new BaseResponse();
response.setErrorcode(1);
response.setErrormsg("请勿重复提交");
return response;
}
}
finally
{
//设置解锁key和解锁时间
ResubmitLock.getInstance().unLock(lock, key.toString(), delaySeconds);
}
}
}
return joinPoint.proceed();
}

}