依赖

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

使用demo

使用Guava Retrying可以简单分为三步,(1)通过RetryerBuilder工厂类构造Retryer(2)需要重试的动作实现callable接口(3)调用call方法

RetryerBuilder


@Component
public class AppRetryerBuilder { public Retryer<Boolean> build() {
return RetryerBuilder.<Boolean>newBuilder()
//如果result为false则重试
.retryIfResult(i -> i.equals(false))
//如果执行中有异常则重试
.retryIfException()
//每次重试之间间隔三秒
.withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS))
//最大重试次数,当达到这个次数则抛出ExecutionException异常
.withStopStrategy(StopStrategies.stopAfterAttempt(10))
//RetryListener可以监控多次重试过程,并可以使用attempt做一些额外的事情
.withRetryListener(new RetryLogListener())
.build(); } }

实现callable接口

        Callable<Boolean> callable = () -> {
log.info("call times={}", times);
times++;
if (times == 2) {
throw new RuntimeException();
} else if (times == 3) {
throw new Exception();
} else if (times == 4) {
return false;
} else return times != 5;
};

调用

        try {
Boolean call = retryer.call(callable);//也可以不用try,call方法抛出的是运行时异常
System.out.println(call);
} catch (Exception e) {
e.printStackTrace();
}

git

https://github.com/lexiaoyao1995/retry

参考

https://www.jianshu.com/p/557eb67bb3d8

最新文章

  1. SQL 语言 - 数据库系统原理
  2. JAVA 注解的几大作用及使用方法详解
  3. Android 实现简单音乐播放器(二)
  4. 【Git】笔记1
  5. javascript通过时区获取时间
  6. HTML5[7]: 实现网页版的加载更多
  7. ssm中使用hibernate-validator验证BO
  8. WPF 样式和行为
  9. 未能加载文件或程序集“Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad
  10. 64bit Ubuntu, Android AAPT, R.java
  11. Swift学习笔记 - 函数与闭包
  12. asp.net core VS goang web[修正篇]
  13. Visual Studio 2013进行单元测试
  14. Temperature hdu 3477
  15. Golang开发者常见的坑
  16. Python学习笔记 变量
  17. WPF 依赖属性源码 洞察微软如何实现DependencyProperty
  18. DDD实战进阶第一波(二):开发一般业务的大健康行业直销系统(搭建支持DDD的轻量级框架一)
  19. BZOJ_3626_[LNOI2014]LCA_离线+树剖
  20. MUI 实用教程

热门文章

  1. 说说Spring中的 @RestController 和 @Controller
  2. IDEA中配置Maven
  3. 2020-03-27:JDK1.8中在数据结构上,对HashMap做了什么样的改进?为什么?
  4. C#算法设计查找篇之04-斐波那契查找
  5. Vue element-ui el-table阻止行选事件
  6. golang的 strconv 包
  7. Python-Opencv 轮廓常用操作
  8. jmeter性能测试入门使用参数化
  9. springMVC入门(七)------RESTFul风格的支持
  10. 第七天Scrum冲刺博客