当前位置:   article > 正文

注解实现防止重复提交_防重复提交注解

防重复提交注解
  1. @Target({ElementType.METHOD})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface AvoidRepeatableCommit {
  4. long timeout() default 1000L;
  5. }
  1. public enum AvoidCommitAspectErrorEnum implements ErrorEnum {
  2. BASE_AVOID_COMMIT_ASPECT_ERROR("02990001", "請勿重複提交");
  3. private final String code;
  4. private final String message;
  5. public String getCode() {
  6. return this.code;
  7. }
  8. public String getMessage() {
  9. return this.message;
  10. }
  11. private AvoidCommitAspectErrorEnum(String code, String message) {
  12. this.code = code;
  13. this.message = message;
  14. }
  15. }
 
  1. @Aspect
  2. @Component
  3. public class AvoidCommitAspect {
  4. private static final Logger log = LoggerFactory.getLogger(AvoidCommitAspect.class);
  5. @Autowired
  6. private ICacheService cacheService;
  7. public AvoidCommitAspect() {
  8. }
  9. @Around("checkSubmit()")
  10. public Object around(ProceedingJoinPoint point) throws Throwable {
  11. HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
  12. String ip = IpUtil.getIpAddr(request);
  13. MethodSignature signature = (MethodSignature)point.getSignature();
  14. Method method = signature.getMethod();
  15. String className = method.getDeclaringClass().getName();
  16. String name = method.getName();
  17. StringBuilder param = new StringBuilder("param");
  18. Object[] var9 = point.getArgs();
  19. int hashCode = var9.length;
  20. for(int var11 = 0; var11 < hashCode; ++var11) {
  21. Object object = var9[var11];
  22. param.append(JsonUtil.toJsonString(object));
  23. }
  24. String ipKey = String.format("%s#%s#%s", className, name, param.toString());
  25. hashCode = ipKey.hashCode();
  26. String key = String.format("%s_%d", ip, hashCode);
  27. log.info("ipKey={},hashCode={},key={}", new Object[]{ipKey, hashCode, key});
  28. AvoidRepeatableCommit avoidRepeatableCommit = (AvoidRepeatableCommit)method.getAnnotation(AvoidRepeatableCommit.class);
  29. long timeout = avoidRepeatableCommit.timeout();
  30. String value = (String)this.cacheService.get(key);
  31. if (StringUtil.isNotBlank(value)) {
  32. AvoidCommitAspectErrorEnum.BASE_AVOID_COMMIT_ASPECT_ERROR.fail(new Object[0]);
  33. }
  34. this.cacheService.set(key, UUIDGenerateUtil.getNextId(), timeout, TimeUnit.MILLISECONDS);
  35. return point.proceed();
  36. }
  37. @Pointcut("@annotation(com.teleone.cloud.base.common.AvoidRepeatableCommit)")
  38. public void checkSubmit() {
  39. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/234579
推荐阅读
相关标签
  

闽ICP备14008679号