Guava Retry 进行重试

Guava Retry具有更强的灵活性,可以根据返回值校验来判断是否需要进行重试
首先是pom 文件

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

然后就是重试类了

 public Retryer<T> objectRetried(){
        return RetryerBuilder.<T>newBuilder()
                .retryIfExceptionOfType(Exception.class) //抛出指定异常已经异常子类重试
                .retryIfResult(ObjectUtils::isEmpty) //不满足条件重试
                .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS)) //休眠时间
                .withStopStrategy(StopStrategies.stopAfterAttempt(3)) //停止策略
                .build();
    }

然后就是引用了
Object retryer = retryService.stringRetried().call(() -> tenantService.getById(9999)); 很遗憾的是只针对某个方法进行重试

posted @ 2021-03-16 23:34  lyj98  阅读(384)  评论(0)    收藏  举报