1、添加pom依赖

<dependency>
<groupId>com.github.rholder</groupId>
<artifactId>guava-retrying</artifactId>
<version>2.0.0</version>
</dependency>

2、Guava Retry具体使用

public void guavaRetry(String param) {
Retryer<Boolean> retry = RetryerBuilder.<Boolean>newBuilder()
.retryIfResult(Predicates.equalTo(false))//设置根据结果重试
.retryIfException()//设置异常重试源
.withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))//设置等待间隔时间(失败后,将等待固定的时长进行重试)
.withStopStrategy(StopStrategies.stopAfterAttempt(5))// 重试停止策略:设置最大重试次数
.withRetryListener(new RetryListener() {
@Override
public <V> void onRetry(Attempt<V> attempt) {
System.out.println("准备开始第几次重试:"+attempt.getAttemptNumber());
System.out.println("是异常导致的重试还是正常返回:"+attempt.hasException());
System.out.println("如果是异常导致的重试,那么获取具体具体的异常类型:"+attempt.getExceptionCause());
}
})
.build();
AtomicReference<String> message = new AtomicReference<>("");
try {
retry.call(()->{
System.out.println("尝试");
String method = this.method();//该方法出现异常后,后面代码不会继续执行
if (!"成功".equals(method)) {
message.set("失败");
}else {
message.set("");
}
return true;
});
} catch (Exception e) {
message.set("失败");//整个尝试失败
e.printStackTrace();
}finally {
//尝试一定次数后的最终处理
System.out.println(message.get()+"++++++++++");
}
}

搜索

复制

最新文章

  1. 关于oracle 10g creating datafile with zero offset for aix
  2. Rest风格中关于JPA使用懒加载的坑
  3. Android编程: MVC模式、应用的生命周期
  4. Ubuntu 12.04 Desktop使用XAMPP
  5. BZOJ 2751 容易题
  6. Java多线程性能优化
  7. fedora安装sublime text教程
  8. 【算法】计算一篇文章的单词数(C、Java语言实现)
  9. 使用vbs脚本添加域网络共享驱动器
  10. 产品经理必备工具-Axure(1)
  11. SpringMVC(一):搭建一个SpringMVC helloword项目
  12. 高通msm8994性能及温度监测脚本
  13. element-UI的Dialog弹出框蒙版被遮住
  14. Mongodb 相关链接
  15. Django+wechatpy接入微信公众平台以及授权登录
  16. 使用eclipse初步学习vue.js基础==》v-for的使用 ②
  17. 【读书笔记】iOS-iOS开发之iOS程序偏好设置(Settings Bundle)的使用
  18. CSS3和jQuery实现的自定义美化Checkbox和Radiobox
  19. Session实例
  20. mysql的字符拼接

热门文章

  1. Mac 压缩软件Keka
  2. CS客户端 App.Config更新问题
  3. ETCD 实现服务发现讲解
  4. java学习一:java介绍及第一个helloword程序
  5. Python与CSharp之间内存共享互传信息
  6. Java_用数组保存并显示杨辉三角
  7. 16.java八皇后问题
  8. Go 在 linux 上安装
  9. 带有关键词的行 txt文本处理
  10. groupByKey、reduceByKey、aggregateByKey、foldByKey、combineByKey的联系和区别