Skip to content
Advertisement

How to avoid hardcode in every class WebClient retryWhen

I want to implement retry mechanism and I did something like this:

  public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
      .onRetryExhaustedThrow(
          (retryBackoffSpec, retrySignal) -> new TimeoutException(
              retrySignal.failure().getMessage()));

And used this method here:

 public List<A> findByTimestamp(LocalDate localDate) {
    return webClient.get()
        .uri(bProp.getPath())
        .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
        .retrieve()
        .bodyToFlux(bData.class)
        .retryWhen(fixedRetry)
        .map(this::toC)
        .collectList()
        .block();
  }

But I want to create a generic one to use it across all application to not write first method in all classes, how can I do this more efficiently?

Advertisement

Answer

The way that I decided to implement this is as follows:

public class RetryWebClient {

  public static final Retry fixedRetry = Retry.fixedDelay(3, Duration.ofSeconds(5))
      .onRetryExhaustedThrow(
          (retryBackoffSpec, retrySignal) -> new TimeoutException(
              retrySignal.failure().getMessage()));

  private RetryWebClient() {}

}

And called it in retryWhen:

.retryWhen(RetryWebClient.fixedRetry)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement