🌶️ 重试机制
ForestRequest 对象提供了设置重试相关属性的方法
是否运行重试 (默认为开启重试)
setRetryEnabled(boolean retryEnabled)设置是否开启请求重试
- 参数
 retryEnabled:true开启重试,false关闭重试
// 开启请求重试(默认即开启)
request.setRetryEnabled(true);
// 关闭请求重试(关闭后该请求不会再触发重试)
request.setRetryEnabled(false);
 1
2
3
4
2
3
4
设置最大重试次数 (默认为0次,即不会重试)
maxRetryCount(int retryCount)设置请求失败后的最大重试次数
- 参数
 retryCount: 重试次数
// 设置请求最大重试次数为 3 次
request.maxRetryCount(3);
 1
2
2
设置最大请重试的时间间隔 (时间单位为毫秒, 默认为0毫秒)
maxRetryInterval(long maxRetryInterval)设置最大请重试的时间间隔
- 参数
 maxRetryInterval: 最大请重试的时间间隔 (毫秒)
// 设置请求最大重试次数为 10ms
request.maxRetryInterval(10L);
 1
2
2
# 重试器
Retryer 重试器,即重试策略,可以设定每次重试请求之间的时间间隔
Forest 默认重试器类为 com.dtflys.forest.retryer.BackOffRetryer,它是依据二进制退避算法的重试策略类
若配置该重试器,重试过程如下:
- 第一次重试与第一次请求之间间隔 0的2次方 * 1s, 即0s
 - 第二次重试与第一次重试之间间隔 1的2次方 * 1s, 即1s
 - 第三次次重试与第二次重试之间间隔 2的2次方 * 1s, 即4s
 - 后面几次重试时间间隔以此类推,直到达到最大请求次数后停止重试
 - 每次时间间隔不能大于 
maxRetryInterval, 若maxRetryInterval设置为10, 则每次间隔只能为10ms 
您也可以自定义重试器
// 自定义重试器
// 继承 BackOffRetryer 类
public class MyRetryer extends BackOffRetryer {
    public MyRetryer(ForestRequest request) {
        super(request);
    }
    /**
     * 重写 nextInterval 方法
     * 该方法用于指定每次重试的时间间隔
     * @param currentCount 当前重试次数
     * @return 时间间隔 (时间单位为毫秒)
     */
    @Override
    protected long nextInterval(int currentCount) {
        // 每次重试时间间隔恒定为 1s (1000ms)
        return 1000;
    }
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
再通过 ForestRequest 对象的 retryer(Class<? extends ForestRetryer> retryerClass) 设置该自定义重试器类
// 设置自定义重试器类
request.retryer(MyRetryer.class);
 1
2
2
# 重试条件
Forest 请求的重试条件有两种设置模式:
- 将 SuccessWhen 请求成功/失败条件 作为重试条件
 - 设置 RetryWhen 重试条件
 
一般情况下不会设置 RetryWhen 重试条件,即直通过请求的成功/失败来判断是否重试,逻辑很简单:请求成功不重试,失败就重试
但有些特殊情况,需要在请求成功的情况下也重试,满足一定业务条件后才停止重试,这种情况就需要 RetryWhen 重试条件上场了
retryWhen(RetryWhen retryWhen)设置重试条件:用于判断请求是否触发重试
- 参数
 retryWhen: RetryWhen 接口实例
Forest.get("/")
     // 最大重试次数为 3
    .maxRetryCount(3)
     // 最大重试间隔为 10ms
    .maxRetryInterval(10)
     // 重试条件: 状态码为 203 就重试
    .retryWhen(((req, res) -> res.statusIs(203)))
     // onSuccess回调函数: 成功时调用
    .onSuccess((data, req, res) -> {
        System.out.println("成功!")
    })
     // 执行请求
    .execute();
// 若发送请求后,服务端返回 203 状态码
// 就不断触发重试
// 直到服务端不返回 203,或达到最大重试次数,停止重试
// 若最后一次重试服务端发送的还是 203,则认为请求成功,执行 onSuccess
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
先定义自定义请求重试实现类
// 自定义重试条件类
// 需要实现 RetryWhen 接口
public class MyRetryCondition implements RetryWhen {
    /**
     * 请求重试条件
     * @param req Forest请求对象
     * @param res Forest响应对象
     * @return true 重试,false 不重试
     */
    @Override
    public boolean retryWhen(ForestRequest req, ForestResponse res) {
        // 响应状态码为 203 就重试
        return res.statusIs(203);
    }
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
再通过调用 retryWhen(Class<? extends RetryWhen> conditionClass) 方法设置自定义重试条件类
Forest.get("/")
     // 最大重试次数为 3
    .maxRetryCount(3)
     // 最大重试间隔为 10ms
    .maxRetryInterval(10)
     // 重试条件: 状态码为 203 就重试
    .retryWhen(MyRetryCondition.class)
     // onSuccess回调函数: 成功时调用
    .onSuccess((data, req, res) -> {
        System.out.println("成功!");
    })
     // 执行请求
    .execute();
// 若发送请求后,服务端返回 203 状态码
// 就不断触发重试
// 直到服务端不返回 203,或达到最大重试次数,停止重试
// 若最后一次重试服务端发送的还是 203,则认为请求成功,执行 onSuccess
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
帮助我们改善此文档  (opens new window)
  上次更新: 2024/12/26, 12:59:11