Java实现幂等注解

拦截器

@Aspect
@Component
public class NoRepeatAspect {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Pointcut("@annotation(com.sunline.project.aop.NoRepeat) || @within(com.sunline.project.aop.NoRepeat)")
    public void pointCut(){}


    @Around("pointCut()")
    private Object around(ProceedingJoinPoint point) {
        try {
            // 获取当前用户的token
            Subject currStaff = SecurityUtils.getSubject();
            UserVo user = (UserVo) currStaff.getPrincipal();
            
            String token = user.getToken();
            StaticLog.info("检测是否重复提交");
            StaticLog.info("point:{}", point);

            // 获取当前请求的方法名
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            String name = method.getName();
            StaticLog.info("token:{},======methodName:{}", token, name);
            if (redisTemplate.hasKey(token+name)) {
                StaticLog.error("监测到重复提交>>>>>>>>>>");
                return ResponseData.fail(ResponseCode.FAIL_CODE, "请勿重复提交");
            }
            // 获取注解
            NoRepeat annotation = method.getAnnotation(NoRepeat.class);
            Long timeout = annotation.timeOut();
            // 此处我用token和请求方法名为key存入redis中,有效期为timeout 时间, 也可以使用ip地址做为key
            redisTemplate.opsForValue().set(token+name, token+name, timeout, TimeUnit.SECONDS);
            return point.proceed();
        } catch (Throwable throwable) {
            StaticLog.error("=====>>>>>>操作失败:{}", throwable);
            return ResponseData.fail(ResponseCode.FAIL_CODE, "操作失败");
        }
    }
}

注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NoRepeat {
    // 默认失效时间
    long timeOut() default 10;
}

测试:@NoRepeat(timeOut = 5)
timeOut默认时间为10s

    @NoRepeat(timeOut = 5)
    @GetMapping(value = "/test")
    @ApiOperation(value = "测试幂等注解")
    @SysLogs("测试幂等注解")
    @ApiImplicitParam(paramType = "header", name = "Authorization", value = "身份认证Token")
    public ResponseData<T> testIdempotent() {
        try {
            System.err.println(new Date());
            return ResponseData.ok(ResponseCode.SUCCESS_CODE, "操作成功");
        } catch (Exception e) {
            StaticLog.error("操作失败:{}", e);
            return ResponseData.ok(ResponseCode.FAIL_CODE, "操作失败");
        }
    }
上一篇:linux rz命令无效


下一篇:Ubuntu安装sz/rz命令以及用法