达到最大弹性尝试次数后抛出异常

时间:2019-07-28 16:50:27

标签: groovy retry-logic resilience4j

达到最大重试次数时如何引发异常。 就我而言,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()

1 个答案:

答案 0 :(得分: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);