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
<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:
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;
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;
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder() .retryIfExceptionOfType(IOException.class) .retryIfRuntimeException() .withWaitStrategy(WaitStrategies.fibonacciWait(100, 2, TimeUnit.MINUTES)) .withStopStrategy(StopStrategies.neverStop()) .build();
标签:retry,Retryer,retrying,call,IOException,guava From: https://www.cnblogs.com/anpeiyong/p/18131205