zl程序教程

您现在的位置是:首页 >  Java

当前栏目

更好的 Java 重试框架 Sisyphus 配置的两种方式介绍

2023-04-18 15:44:24 时间

这一节让我们一起学习下 sisyphus 基于函数式的配置和注解式的配置。

函数式配置概览

为了满足更加方便的配置,Retryer 类提供了许多可以配置的信息。

默认配置

  1. /** 
  2.  * 默认配置测试 
  3.  */ 
  4. public void defaultConfigTest() { 
  5.     Retryer.<String>newInstance() 
  6.             .condition(RetryConditions.hasExceptionCause()) 
  7.             .retryWaitContext(RetryWaiter.<String>retryWait(NoRetryWait.class).context()) 
  8.             .maxAttempt(3) 
  9.             .listen(RetryListens.noListen()) 
  10.             .recover(Recovers.noRecover()) 
  11.             .callable(new Callable<String>() { 
  12.                 @Override 
  13.                 public String call() throws Exception { 
  14.                     System.out.println("called..."); 
  15.                     throw new RuntimeException(); 
  16.                 } 
  17.             }).retryCall(); 

 和下面的代码是等价的:

  1. public void helloTest() { 
  2.     Retryer.<String>newInstance() 
  3.             .callable(new Callable<String>() { 
  4.                 @Override 
  5.                 public String call() throws Exception { 
  6.                     System.out.println("called..."); 
  7.                     throw new RuntimeException(); 
  8.                 } 
  9.             }).retryCall(); 

方法说明

condition

重试触发的条件,可以指定多个条件。

默认为抛出异常。

retryWaitContext

重试等待的策略,可以指定多个。

默认为不做任何等待。

maxAttempt

指定最大重试次数,包括第一次执行。

默认值:3 次。

listen

指定重试的监听实现,默认为不做监听。

recover

当重试完成之后,依然满足重试条件,则可以指定恢复的策略。

默认不做恢复。

callable

待重试执行的方法。

retryCall

触发重试执行。

接口的详细介绍

接口及其实现

所有的接口,都可以直接查看对应的子类实例。

用户自定义

基于替换的灵活性,用户可以实现接口,定义更符合自己业务的实现。

sisyphus 注解

配置具有很高的灵活性,但是对于开发人员的使用,就没有注解那样简单灵活。

所以本框架也实现了基于注解的重试。

设计的规范

保证接口和注解二者的统一性。

maven 引入

  1. <dependency> 
  2.     <groupId>${project.groupId}</groupId> 
  3.     <artifactId>sisyphus-annotation</artifactId> 
  4.     <version>${project.version}</version> 
  5. </dependency> 

注解

核心注解主要有两个。

Retry

用于指定重试的相关配置。

  1. /** 
  2.  * 重试注解 
  3.  * 1. 实际需要,只允许放在方法上。 
  4.  * 2. 如果放在接口上,是否所有的子类都生效?为了简单明确,不提供这种实现。 
  5.  * 3. 保持注解和接口的一致性。{@link com.github.houbb.sisyphus.api.core.Retry} 接口 
  6.  * @author binbin.hou 
  7.  * @since 0.0.3 
  8.  */ 
  9. @Documented 
  10. @Inherited 
  11. @Target(ElementType.METHOD) 
  12. @Retention(RetentionPolicy.RUNTIME) 
  13. @RetryAble(DefaultRetryAbleHandler.class) 
  14. public @interface Retry { 
  15.  
  16.     /** 
  17.      * 重试类实现 
  18.      * @return 重试 
  19.      * @since 0.0.5 
  20.      */ 
  21.     Class<? extends com.github.houbb.sisyphus.api.core.Retry> retry() default DefaultRetry.class; 
  22.  
  23.     /** 
  24.      * 最大尝试次数 
  25.      * 1. 包含方法第一次正常执行的次数 
  26.      * @return 次数 
  27.      */ 
  28.     int maxAttempt() default 3; 
  29.  
  30.     /** 
  31.      * 重试触发的场景 
  32.      * @return 重试触发的场景 
  33.      */ 
  34.     Class<? extends RetryCondition> condition() default ExceptionCauseRetryCondition.class; 
  35.  
  36.     /** 
  37.      * 监听器 
  38.      * 1. 默认不进行监听 
  39.      * @return 监听器 
  40.      */ 
  41.     Class<? extends RetryListen> listen() default NoRetryListen.class; 
  42.  
  43.     /** 
  44.      * 恢复操作 
  45.      * 1. 默认不进行任何恢复操作 
  46.      * @return 恢复操作对应的类 
  47.      */ 
  48.     Class<? extends Recover> recover() default NoRecover.class; 
  49.  
  50.     /** 
  51.      * 等待策略 
  52.      * 1. 支持指定多个,如果不指定,则不进行任何等待, 
  53.      * @return 等待策略 
  54.      */ 
  55.     RetryWait[] waits() default {}; 
  56.  

RetryWait

用于指定重试的等待策略。

  1. package com.github.houbb.sisyphus.annotation.annotation; 
  2.  
  3. import com.github.houbb.sisyphus.annotation.annotation.metadata.RetryWaitAble; 
  4. import com.github.houbb.sisyphus.annotation.handler.impl.DefaultRetryWaitAbleHandler; 
  5. import com.github.houbb.sisyphus.core.constant.RetryWaitConst; 
  6. import com.github.houbb.sisyphus.core.support.wait.NoRetryWait; 
  7.  
  8. import java.lang.annotation.*; 
  9.  
  10. /** 
  11.  * 重试等待策略 
  12.  * 1. 为了对应重试策略,所有的内置注解应该实现当前的注解。 
  13.  * 2. 是否允许自定义注解? 
  14.  * 
  15.  * 当注解+对象同时出现的时候,视为组合。 
  16.  * 
  17.  * @author binbin.hou 
  18.  * @since 0.0.3 
  19.  */ 
  20. @Retention(RetentionPolicy.RUNTIME) 
  21. @Inherited 
  22. @Documented 
  23. @Target(ElementType.ANNOTATION_TYPE) 
  24. @RetryWaitAble(DefaultRetryWaitAbleHandler.class) 
  25. public @interface RetryWait { 
  26.  
  27.     /** 
  28.      * 默认值 
  29.      * 1. fixed 模式,则对应固定等待时间 
  30.      * 2. 递增 
  31.      * @return 默认值 
  32.      */ 
  33.     long value() default RetryWaitConst.VALUE_MILLS; 
  34.  
  35.     /** 
  36.      * 最小值 
  37.      * @return 最小值 
  38.      */ 
  39.     long min() default RetryWaitConst.MIN_MILLS; 
  40.  
  41.     /** 
  42.      * 最大值 
  43.      * @return 最大值 
  44.      */ 
  45.     long max() default RetryWaitConst.MAX_MILLS; 
  46.  
  47.     /** 
  48.      * 影响因数 
  49.      * 1. 递增重试,默认为 {@link RetryWaitConst#INCREASE_MILLS_FACTOR} 
  50.      * 2. 指数模式。默认为 {@link RetryWaitConst#MULTIPLY_FACTOR} 
  51.      * @return 影响因数 
  52.      */ 
  53.     double factor() default Double.MIN_VALUE; 
  54.  
  55.     /** 
  56.      * 指定重试的等待时间 class 信息 
  57.      * @return 重试等待时间 class 
  58.      */ 
  59.     Class<? extends com.github.houbb.sisyphus.api.support.wait.RetryWait> retryWait() default NoRetryWait.class; 
  60.  

注解的使用

定义好了注解,肯定要有注解的相关使用。

关于注解的使用,主要有两种方式。

Proxy+CGLIB

基于代理模式和字节码增强。

如果是项目中没有使用 spring,直接使用这种方式比较方便。

Spring-AOP

可以和 spring 直接整合。

使用方式和 spring-retry 是一样的。

这些内容将放在下一节进行详细讲解。

小结

灵活的配置才能更加符合实际生产使用中的各种需求。

一般实际使用推荐使用注解的配置方式,非常的简单方便。