定义:一个重试机制的框架。
使用方式:
- 添加依赖
<dependency>
<groupId>com.github.rholder</groupId>
<artifactId>guava-retrying</artifactId>
<version>2.0.0</version>
</dependency>
- 创建重试器,执行重试方法
package com.pjk.demo;
import com.github.rholder.retry.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* @author PJK
*/
public class Demo_GuavaRetry {
public static int a = 3;
public static int i=0;
public static void main(String[] args) {
// 创建重试器
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfResult(result-> result!=null && !result)
.withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt(10))
.build();
try {
// 调用call方法 异步执行 重试任务
retryer.call(() -> {
a--;
i++;
if (a == 0) {
System.out.println("结果为0,终止尝试");
return true;
} else {
System.out.println("第"+i+"次尝试");
return false;
}
});
} catch (ExecutionException | RetryException e) {
System.out.println(e);
}
}
}
标签:Retry,System,重试,static,result,import,Guava,public
From: https://blog.51cto.com/u_16205743/7502515