在 resilience4j-retry 中达到最大尝试次数后抛出异常

Throw exception after reaching max attempts in resilience4j-retry

如何在达到最大重试次数时抛出异常。 在我的例子中,当 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()

当 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);