guava-retrying重试工具库: 什么时候终止
当我们重试到一定阶段的时候,需要终止重试过程,比如重试了n次或者重试了n秒等。
StopStrategies.stopAfterAttempt(n):在重试了n次后终止,这个实际中最常用。
StopStrategies.neverStop():从不终止,一直重试,没有什么实际用处。
StopStrategies.stopAfterDelay(10, TimeUnit.SECONDS):一直重试,指定时间过后终止。
guava-retrying提供了StopStrategy接口,我们可以实现自己想要的终止逻辑。
public interface StopStrategy {
boolean shouldStop(Attempt var1);
}
下面我们使用自己实现的终止策略,重试1~11间的随机次数后终止。
参考资料:
评论已关闭