张北网站seo,网站 会员管理,网站建设等级定级,创世网站建设上次写过一篇如何使用spring retry来实现业务重试的文章#xff1a;https://blog.csdn.net/Kingsea442/article/details/135341747 尽管 Spring Retry 工具能够优雅地实现重试#xff0c;但它仍然存在两个不太友好的设计#xff1a;
重试实体被限定为 Throwable 子类#… 上次写过一篇如何使用spring retry来实现业务重试的文章https://blog.csdn.net/Kingsea442/article/details/135341747 尽管 Spring Retry 工具能够优雅地实现重试但它仍然存在两个不太友好的设计
重试实体被限定为 Throwable 子类这意味着重试针对的是可捕获的功能异常但实际上我们可能希望依赖某个数据对象实体作为重试实体但是 Spring Retry 框架必须强制将其转换为 Throwable 子类。重试根源的断言对象使用的是 doWithRetry 的 Exception 异常实例这不符合正常内部断言的返回设计。 Spring Retry 建议使用注解来对方法进行重试重试逻辑是同步执行的。重试的“失败”是指 Throwable 异常如果你要通过返回值的某个状态来判断是否需要重试则可能需要自己判断返回值并手动抛出异常。
Guava Retrying 开源地址https://github.com/rholder/guava-retrying Guava Retrying 是基于 Google 的核心类库 Guava 的重试机制实现的一个库它提供了一种通用方法可以使用 Guava 谓词匹配增强的特定停止、重试和异常处理功能来重试任意 Java 代码。这个库支持多种重试策略比如指定重试次数、指定重试时间间隔等。此外它还支持谓词匹配来确定是否应该重试以及在重试时应该做些什么。Guava Retrying 的最大特点是它能够灵活地与其他 Guava 类库集成这使得它非常易于使用。
pom依赖 // maven依赖dependencygroupIdcom.github.rholder/groupIdartifactIdguava-retrying/artifactIdversion2.0.0/version/dependency// gradle依赖compile com.github.rholder:guava-retrying:2.0.0测试demo public static void main(String[] args) {CallableBoolean callable new CallableBoolean() {Overridepublic Boolean call() throws Exception {// do something useful herelog.info(call...);throw new RuntimeException();}};RetryerBoolean retryer RetryerBuilder.BooleannewBuilder()//retryIf 重试条件.retryIfException().retryIfRuntimeException().retryIfExceptionOfType(Exception.class).retryIfException(Predicates.equalTo(new Exception())).retryIfResult(Predicates.equalTo(false))//等待策略每次请求间隔1s.withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))//停止策略 : 尝试请求6次.withStopStrategy(StopStrategies.stopAfterAttempt(6))//时间限制 : 某次请求不得超过2s .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(2, TimeUnit.SECONDS))//注册一个自定义监听器可以实现失败后的兜底方法.withRetryListener(new MyRetryListener()).build();try {retryer.call(callable);} catch (Exception ee) {ee.printStackTrace();}
}当发生重试之后假如我们需要做一些额外的处理动作比如发个告警邮件啥的那么可以使用RetryListener。每次重试之后guava-retrying会自动回调我们注册的监听。可以注册多个RetryListener会按照注册顺序依次调用。
public class MyRetryListener implements RetryListener {Overridepublic V void onRetry(AttemptV attempt) {// 第几次重试System.out.print([retry]time attempt.getAttemptNumber());// 距离第一次重试的延迟System.out.print(,delay attempt.getDelaySinceFirstAttempt());// 重试结果: 是异常终止, 还是正常返回System.out.print(,hasException attempt.hasException());System.out.print(,hasResult attempt.hasResult());// 是什么原因导致异常if (attempt.hasException()) {System.out.print(,causeBy attempt.getExceptionCause().toString());// do something useful here} else {// 正常返回时的结果System.out.print(,result attempt.getResult());}System.out.println();}
}RetryerBuilder是一个factory创建者可以定制设置重试源且可以支持多个重试源可以配置重试次数或重试超时时间以及可以配置等待时间间隔创建重试者Retryer实例。
RetryerBuilder的重试源支持Exception异常对象 和自定义断言对象通过retryIfException 和retryIfResult设置同时支持多个且能兼容。 retryIfException抛出runtime异常、checked异常时都会重试但是抛出error不会重试。
retryIfRuntimeException只会在抛runtime异常的时候才重试checked异常和error都不重试。
retryIfExceptionOfType允许我们只在发生特定异常的时候才重试比如NullPointerException和IllegalStateException都属于runtime异常也包括自定义的error。
retryIfResult可以指定你的Callable方法在返回值的时候进行重试。
StopStrategy停止重试策略提供以下方式
StopAfterDelayStrategy: 设定一个最长允许的执行时间比如设定最长执行10s无论任务执行次数只要重试的时候超出了最长时间则任务终止并返回重试异常 NeverStopStrategy: 用于需要一直轮训知道返回期望结果的情况。 StopAfterAttemptStrategy: 设定最大重试次数如果超出最大重试次数则停止重试并返回重试异常。 WaitStrategy: 等待时长策略控制时间间隔 FixedWaitStrategy: 固定等待时长策略。 RandomWaitStrategy: 随机等待时长策略可以提供一个最小和最大时长等待时长为其区间随机值 IncrementingWaitStrategy: 递增等待时长策略提供一个初始值和步长等待时间随重试次数增加而增加。 ExponentialWaitStrategy: 指数等待时长策略 FibonacciWaitStrategy: 等待时长策略 ExceptionWaitStrategy: 异常时长等待策略 CompositeWaitStrategy: 复合时长等待策略
总结
Guava Retryer 工具与 Spring Retry 类似都是通过定义重试者角色来包装正常逻辑重试。然而Guava Retryer 在策略定义方面更优秀。它不仅支持设置重试次数和重试频度控制还能够兼容多个异常或自定义实体对象的重试源定义从而提供更多的灵活性。这使得 Guava Retryer 能够适用于更多的业务场景比如网络请求、数据库访问等。此外Guava Retryer 还具有很好的可扩展性可以很方便地与其他 Guava 类库集成使用。