Class RetryUtil
java.lang.Object
com.sure.tool.util.RetryUtil
重试工具类:按固定间隔重试指定次数,支持结果条件与异常判定,零依赖。
典型场景:网络请求、远程调用等瞬态失败后的指数或固定间隔重试。
- Since:
- 0.2.0
- Author:
- suretool
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean固定间隔重试,直到成功或达到最大次数。static boolean固定间隔重试,直到成功或达到最大次数。static <T> TretryUntil(Callable<T> callable, Predicate<T> condition, int maxAttempts, Duration interval) 重试直到结果满足条件。
-
Method Details
-
retry
public static boolean retry(Callable<Boolean> callable, int maxAttempts, Duration interval) throws InterruptedException 固定间隔重试,直到成功或达到最大次数。- Parameters:
callable- 业务操作(返回 Boolean 或 true 表示成功)maxAttempts- 最大尝试次数(含首次,>0)interval- 每次重试间隔- Returns:
- 是否在重试次数内成功
- Throws:
InterruptedException- 线程中断
-
retry
public static boolean retry(Callable<Boolean> callable, int maxAttempts, Duration interval, boolean retryOnError) throws InterruptedException 固定间隔重试,直到成功或达到最大次数。- Parameters:
callable- 业务操作maxAttempts- 最大尝试次数(含首次,>0)interval- 每次重试间隔retryOnError- 是否在异常时继续重试(false 则异常立即抛出)- Returns:
- 是否在重试次数内成功
- Throws:
InterruptedException- 线程中断
-
retryUntil
public static <T> T retryUntil(Callable<T> callable, Predicate<T> condition, int maxAttempts, Duration interval) throws InterruptedException 重试直到结果满足条件。- Type Parameters:
T- 结果类型- Parameters:
callable- 业务操作condition- 成功条件(对结果断言)maxAttempts- 最大尝试次数(含首次)interval- 重试间隔- Returns:
- 满足条件的结果;全部失败返回最后一次结果
- Throws:
InterruptedException- 线程中断
-