@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AvoidRepeatableCommit {
long timeout() default 1000L;
}
public enum AvoidCommitAspectErrorEnum implements ErrorEnum {
BASE_AVOID_COMMIT_ASPECT_ERROR("02990001", "請勿重複提交");
private final String code;
private final String message;
public String getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
private AvoidCommitAspectErrorEnum(String code, String message) {
this.code = code;
this.message = message;
}
}
@Aspect
@Component
public class AvoidCommitAspect {
private static final Logger log = LoggerFactory.getLogger(AvoidCommitAspect.class);
@Autowired
private ICacheService cacheService;
public AvoidCommitAspect() {
}
@Around("checkSubmit()")
public Object around(ProceedingJoinPoint point) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
String ip = IpUtil.getIpAddr(request);
MethodSignature signature = (MethodSignature)point.getSignature();
Method method = signature.getMethod();
String className = method.getDeclaringClass().getName();
String name = method.getName();
StringBuilder param = new StringBuilder("param");
Object[] var9 = point.getArgs();
int hashCode = var9.length;
for(int var11 = 0; var11 < hashCode; ++var11) {
Object object = var9[var11];
param.append(JsonUtil.toJsonString(object));
}
String ipKey = String.format("%s#%s#%s", className, name, param.toString());
hashCode = ipKey.hashCode();
String key = String.format("%s_%d", ip, hashCode);
log.info("ipKey={},hashCode={},key={}", new Object[]{ipKey, hashCode, key});
AvoidRepeatableCommit avoidRepeatableCommit = (AvoidRepeatableCommit)method.getAnnotation(AvoidRepeatableCommit.class);
long timeout = avoidRepeatableCommit.timeout();
String value = (String)this.cacheService.get(key);
if (StringUtil.isNotBlank(value)) {
AvoidCommitAspectErrorEnum.BASE_AVOID_COMMIT_ASPECT_ERROR.fail(new Object[0]);
}
this.cacheService.set(key, UUIDGenerateUtil.getNextId(), timeout, TimeUnit.MILLISECONDS);
return point.proceed();
}
@Pointcut("@annotation(com.teleone.cloud.base.common.AvoidRepeatableCommit)")
public void checkSubmit() {
}