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, "操作失败");
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java自定义注解可以用于实现接口的幂等操作。所谓接口的幂等,就是同一个请求多次执行也不会产生重复的副作用。 要实现接口的幂等,可以通过以下步骤: 1. 定义一个自定义注解,例如@Idempotent,用于标识接口方法的幂等性。 ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Idempotent { } ``` 2. 在接口方法上使用@Idempotent注解,表示该接口方法是幂等的。 ```java public interface MyService { @Idempotent void myMethod(); } ``` 3. 在实现类中,通过AOP的方式实现接口方法的幂等逻辑。可以使用AspectJ或Spring AOP等框架来实现。 ```java @Component @Aspect public class IdempotentAspect { @Autowired private IdempotentService idempotentService; @Around("@annotation(com.example.Idempotent)") public void checkIdempotent(ProceedingJoinPoint joinPoint) throws Throwable { // 获取方法参数,生成幂等key String idempotentKey = generateIdempotentKey(joinPoint.getArgs()); // 判断该key是否已经执行过,如果执行过则直接返回 if (idempotentService.isExecuted(idempotentKey)) { return; } // 执行方法体 joinPoint.proceed(); // 标记该key已经执行过 idempotentService.markExecuted(idempotentKey); } // 生成幂等key的逻辑 private String generateIdempotentKey(Object[] args) { // 根据接口方法的参数生成唯一标识,例如将参数拼接成字符串 return StringUtils.join(args); } } ``` 通过以上步骤,我们就可以实现接口方法的幂等性。在执行接口方法之前,会先判断该方法是否已经执行过,如果已执行过,则直接返回,避免重复执行产生副作用。同时,通过自定义注解@Idempotent的标识,可以简化幂等逻辑的实现和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值