guava-retrying
https://github.com/rholder/guava-retrying
##What is this?
The guava-retrying module provides a general purpose method for retrying arbitrary Java code with specific stop, retry, and exception handling capabilities that are enhanced by Guava's predicate matching.
This is a fork of the excellent RetryerBuilder code posted here by Jean-Baptiste Nizet (JB).
I've added a Gradle build for pushing it up to my little corner of Maven Central so that others can easily pull it into their existing projects with minimal effort.
It also includes exponential and Fibonacci backoff WaitStrategies that might be useful for situations where more well-behaved service polling is preferred.
guava-retrying 为Java提供了 通用的重试(stop、retry、异常处理);
Maven
1 2 3 4 5 | <dependency> <groupId>com.github.rholder</groupId> <artifactId>guava-retrying</artifactId> <version> 2.0 . 0 </version> </dependency> |
Quickstart
A minimal sample of some of the functionality would look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Callable<Boolean> callable = new Callable<Boolean>() { public Boolean call() throws Exception { return true ; // do something useful here } }; Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfResult(Predicates.<Boolean>isNull()) .retryIfExceptionOfType(IOException. class ) .retryIfRuntimeException() .withStopStrategy(StopStrategies.stopAfterAttempt( 3 )) .build(); try { retryer.call(callable); } catch (RetryException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } |
This will retry whenever the result of the Callable
is null, if an IOException
is thrown, or if any other RuntimeException
is thrown from the call()
method.
It will stop after attempting to retry 3 times and throw a RetryException
that contains information about the last failed attempt.
If any other Exception
pops out of the call()
method it's wrapped and rethrown in an ExecutionException
.
call方法的 Callable的结果是null 或 IOException抛出 或 RuntimeException抛出 ,都会进行重试;
尝试3次后 将会stop 且 抛出最后失败尝试的RetryException;
Exponential Backoff
Create a Retryer
that retries forever, waiting after every failed retry in increasing exponential backoff intervals until at most 5 minutes.
After 5 minutes, retry from then on in 5 minute intervals.
创建一个 永久的Retryer,在每次失败后,将会重试 在增长的指数级 间隔,最多5min;
1 2 3 4 5 6 | Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfExceptionOfType(IOException. class ) .retryIfRuntimeException() .withWaitStrategy(WaitStrategies.exponentialWait( 100 , 5 , TimeUnit.MINUTES)) .withStopStrategy(StopStrategies.neverStop()) .build(); |
Fibonacci Backoff
Create a Retryer
that retries forever, waiting after every failed retry in increasing Fibonacci backoff intervals until at most 2 minutes.
After 2 minutes, retry from then on in 2 minute intervals.
创建一个 永久的Retryer,在每次失败后,将会重试 在斐波那契 间隔,最多2min;
1 2 3 4 5 6 | Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfExceptionOfType(IOException. class ) .retryIfRuntimeException() .withWaitStrategy(WaitStrategies.fibonacciWait( 100 , 2 , TimeUnit.MINUTES)) .withStopStrategy(StopStrategies.neverStop()) .build(); |
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | @Configuration public class Config { @Bean public Retryer<Boolean> retryer(){ return RetryerBuilder.<Boolean>newBuilder() .retryIfException() .retryIfRuntimeException() .retryIfExceptionOfType(Exception. class ) .retryIfResult(Predicates.<Boolean>isNull()) .withWaitStrategy(WaitStrategies.fixedWait( 1 , TimeUnit.SECONDS)) // 固定等待时间 .withStopStrategy(StopStrategies.stopAfterAttempt( 3 )) // 尝试请求次数 // .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(2, TimeUnit.SECONDS)) // 请求的最大时限 .withRetryListener( new com.github.rholder.retry.RetryListener() { @Override public <V> void onRetry(Attempt<V> attempt) { System.out.println( "onRetry..." ); } }) .build(); } @Service public class GuavaRetryTest { @Autowired private Retryer<Boolean> retryer; public void test(){ Callable<Boolean> callable = () -> { if ( true ){ throw new TimeoutException(); } return true ; // do something useful here }; try { retryer.call(callable); } catch (RetryException | ExecutionException e) { e.printStackTrace(); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)