1

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

1 回答 1

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);
于 2019-07-31T14:07:46.100 回答