Spring retry重试机制

怼烎@ 2022-01-17 01:01 433阅读 0赞

demo

pom.xml

  1. <dependency>
  2. <groupId>org.springframework.retry</groupId>
  3. <artifactId>spring-retry</artifactId>
  4. <version>1.2.4.RELEASE</version>
  5. </dependency>

测试

  1. public static void main(String[] args) {
  2. RetryTemplate retryTemplate=new RetryTemplate();
  3. retryTemplate.setRetryPolicy(new SimpleRetryPolicy(10));
  4. AtomicInteger counter = new AtomicInteger();
  5. retryTemplate.execute(f->{
  6. if(counter.incrementAndGet()<20){
  7. System.out.println(counter.intValue());
  8. throw new IllegalStateException();
  9. }
  10. return counter.incrementAndGet();
  11. });
  12. }

RetryTemplate默认重试3次,我们可以自定义SimpleRetryPolicy的重试次数

抛出异常是为了重试

总结,当我们重试超过指定次数时候,抛出异常,它会报错的。

Exception in thread “main” java.lang.IllegalStateException
at com.example.demo.Test.lambda$main$0(Test.java:22)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:287)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:164)
at com.example.demo.Test.main(Test.java:19)

所以在重试的时候还要在外层加上try,catch进行相应的处理

或者不加让他直接报错出来我们自己处理

重试注解

aop注解

  1. import java.lang.annotation.*;
  2. @Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
  3. @Retention(RetentionPolicy.RUNTIME) // 保留到运行时,可通过注解获取
  4. @Documented
  5. public @interface MyRetry {
  6. /**
  7. * 重试次数
  8. * @return
  9. */
  10. int times() default 1;
  11. }

aop切面编程

  1. /**
  2. * @author 大鸡腿
  3. * 重试抛出RetryBusinessException,其他异常重试注解不起作用
  4. */
  5. @Slf4j
  6. @Aspect
  7. @Component
  8. public class MyRetryAspect {
  9. @Pointcut("@annotation(com.yatsenglobal.cloud.retry.MyRetry)")
  10. public void annotationPoinCut() {
  11. }
  12. @Around(value = "annotationPoinCut()")
  13. public Object around(ProceedingJoinPoint point) throws Throwable {
  14. //获取方法签名
  15. MethodSignature signature = (MethodSignature) point.getSignature();
  16. //获取切入方法的对象
  17. Method method = signature.getMethod();
  18. //获取方法上的Aop注解
  19. MyRetry myMethod = method.getAnnotation(MyRetry.class);
  20. //重试
  21. RetryTemplate retryTemplate = new RetryTemplate();
  22. retryTemplate.setRetryPolicy(new SimpleRetryPolicy(myMethod.times()));
  23. return retryTemplate.execute(f -> {
  24. Object result;
  25. try {
  26. result = point.proceed();
  27. } catch (RetryBusinessException e) {
  28. throw new RetryBusinessException(ActivityErrorCodeEnum.AC52000200);
  29. }
  30. return result;
  31. });
  32. }
  33. }
  34. RetryBusinessException

这个自己定义

  1. @EqualsAndHashCode(callSuper = true)
  2. @Data
  3. public class RetryBusinessException extends BusinessException {
  4. private static final long serialVersionUID = -8081770607525925770L;
  5. public RetryBusinessException(ActivityErrorCodeEnum codeEnum) {
  6. super(codeEnum.code(), codeEnum.msg());
  7. }
  8. }

发表评论

表情:
评论列表 (有 0 条评论,433人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Retry机制

    在调用第三方接口或者使用mq时,会出现网络抖动,连接超时等网络异常,所以需要重试。为了使处理更加健壮并且不太容易出现故障,后续的尝试操作,有时候会帮助失败的操作最后执行成功。例

    相关 Retry机制

    1、业务场景        应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作。这个功能不复杂,分为两个步骤:第一步调用远程的Rest