Java实现幂等注解
2021/11/12 22:15:26
本文主要是介绍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, "操作失败"); } }
这篇关于Java实现幂等注解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-25初学者必备:订单系统资料详解与实操教程
- 2024-12-24内网穿透资料入门教程
- 2024-12-24微服务资料入门指南
- 2024-12-24微信支付系统资料入门教程
- 2024-12-24微信支付资料详解:新手入门指南
- 2024-12-24Hbase资料:新手入门教程
- 2024-12-24Java部署资料
- 2024-12-24Java订单系统资料:新手入门教程
- 2024-12-24Java分布式资料入门教程
- 2024-12-24Java监控系统资料详解与入门教程