RetryableTask: 任务至少被执行一次, 才会进入重试, 更符合重试工具的语义

This commit is contained in:
Toint
2025-05-18 20:21:32 +08:00
parent b9cedd8798
commit 9f76238137
2 changed files with 12 additions and 13 deletions

View File

@@ -37,7 +37,7 @@ public class RetryUtil {
* 没有返回值,重试执行方法 * 没有返回值,重试执行方法
* *
* @param run 执行方法 * @param run 执行方法
* @param maxAttempts 最大的重试次数 * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次
* @param delay 重试间隔 * @param delay 重试间隔
* @param recover 达到最大重试次数后执行的备用方法 * @param recover 达到最大重试次数后执行的备用方法
* @param exs 指定的异常类型需要重试 * @param exs 指定的异常类型需要重试
@@ -63,7 +63,7 @@ public class RetryUtil {
* 有返回值,重试执行方法 * 有返回值,重试执行方法
* *
* @param sup 执行方法 * @param sup 执行方法
* @param maxAttempts 最大的重试次数 * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次
* @param delay 重试间隔 * @param delay 重试间隔
* @param recover 达到最大重试次数后执行的备用方法 * @param recover 达到最大重试次数后执行的备用方法
* @param exs 指定的异常类型需要重试 * @param exs 指定的异常类型需要重试
@@ -88,10 +88,10 @@ public class RetryUtil {
* 没有返回值,重试执行方法 * 没有返回值,重试执行方法
* *
* @param run 执行方法 * @param run 执行方法
* @param maxAttempts 最大的重试次数 * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次
* @param delay 重试间隔 * @param delay 重试间隔
* @param recover 达到最大重试次数后执行的备用方法 * @param recover 达到最大重试次数后执行的备用方法
* @param predicate 自定义重试条件 * @param predicate 自定义重试条件, 返回true时表示重试
*/ */
public static void ofPredicate(final Runnable run, final long maxAttempts, final Duration delay, public static void ofPredicate(final Runnable run, final long maxAttempts, final Duration delay,
final Supplier<Void> recover, final BiPredicate<Void, Throwable> predicate) { final Supplier<Void> recover, final BiPredicate<Void, Throwable> predicate) {
@@ -105,14 +105,14 @@ public class RetryUtil {
/** /**
* 根据异常信息进行重试 * 根据自定义结果进行重试
* 有返回值,重试执行方法 * 有返回值,重试执行方法
* *
* @param sup 执行方法 * @param sup 执行方法
* @param maxAttempts 最大的重试次数 * @param maxAttempts 最大的重试次数, 小于1不会重试, 但任务至少会被执行1次
* @param delay 重试间隔 * @param delay 重试间隔
* @param recover 达到最大重试次数后执行的备用方法 * @param recover 达到最大重试次数后执行的备用方法
* @param predicate 自定义重试条件 * @param predicate 自定义重试条件, 返回true时表示重试
* @param <T> 结果类型 * @param <T> 结果类型
* @return 执行结果 * @return 执行结果
*/ */

View File

@@ -109,11 +109,11 @@ public class RetryableTask<T> {
*/ */
private T result; private T result;
/** /**
* 执行方法 * 执行方法
*/ */
private final Supplier<T> sup; private final Supplier<T> sup;
/** /**
* 重试策略 * 重试策略, 返回true时表示重试
*/ */
private final BiPredicate<T, Throwable> predicate; private final BiPredicate<T, Throwable> predicate;
/** /**
@@ -150,8 +150,6 @@ public class RetryableTask<T> {
* @return 当前对象 * @return 当前对象
*/ */
public RetryableTask<T> maxAttempts(final long maxAttempts) { public RetryableTask<T> maxAttempts(final long maxAttempts) {
Assert.isTrue(this.maxAttempts > 0, "maxAttempts must be greater than 0");
this.maxAttempts = maxAttempts; this.maxAttempts = maxAttempts;
return this; return this;
} }
@@ -223,7 +221,8 @@ public class RetryableTask<T> {
private RetryableTask<T> doExecute() { private RetryableTask<T> doExecute() {
Throwable th = null; Throwable th = null;
while (--this.maxAttempts >= 0) { // 任务至少被执行一次
do {
try { try {
this.result = this.sup.get(); this.result = this.sup.get();
} catch (final Throwable t) { } catch (final Throwable t) {
@@ -240,7 +239,7 @@ public class RetryableTask<T> {
if (this.maxAttempts > 0) { if (this.maxAttempts > 0) {
ThreadUtil.sleep(delay.toMillis()); ThreadUtil.sleep(delay.toMillis());
} }
} } while (--this.maxAttempts >= 0);
this.throwable = th; this.throwable = th;
return this; return this;