【问题标题】:Throw exception after reaching max attempts in resilience4j-retry在resilience4j-retry中达到最大尝试后抛出异常
【发布时间】:2019-07-28 16:50:27
【问题描述】:

当达到最大重试次数时如何抛出异常。 在我的情况下,当Response 的代码不是 200 时,我想抛出异常。

Retry retry = RetryRegistry.of(
  RetryConfig.<Response> custom()
    .retryOnResult({ it.statusCode() != 200 })
    .build())
  .retry("my-retry")


Response response = Retry.decorateSupplier(retry, { foo.bar() }).get()

【问题讨论】:

    标签: groovy retry-logic resilience4j


    【解决方案1】:

    你可以包装你的代码并在 HTTP 代码不是 200 时抛出异常。

    例如在 Java 代码中:

    Supplier<Response> supplier= () -> foo.bar();
    Supplier<String> supplierWithResultHandling = SupplierUtils.andThen(supplier, result -> {
       if (result.statusCode().is4xxClientError()) {
          throw new HttpClientErrorException(result.statusCode());
       } else if (result.statusCode().is5xxServerError())  {
          throw new HttpServerErrorException(result.statusCode());
       }
      return result;
    });
    
    Response response = retry.executeSupplier(supplierWithResultHandling);
    

    【讨论】:

    • 如果我使用的是spring boot注解怎么办?
    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多