Issue
I'm facing an issue with WebClient
and reactor-extra
. Indeed, I have the following method :
public Employee getEmployee(String employeeId) {
return webClient.get()
.uri(FIND_EMPLOYEE_BY_ID_URL, employeeId)
.retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, clientResponse -> Mono.empty())
.onStatus(HttpStatus::is5xxServerError, clientResponse -> Mono.error(new MyCustomException("Something went wrong calling getEmployeeById")))
.bodyToMono(Employee.class)
.retryWhen(Retry.onlyIf(ConnectTimeoutException.class)
.fixedBackoff(Duration.ofSeconds(10))
.retryMax(3))
.block();
}
I've found that I could use retryWhen(Retry.onlyIf(...))
because I want to retry only if a ConnectTimeoutException
is thrown. I've found this solution from this post : spring webclient: retry with backoff on specific error
But, in the latest version of reactor
the following method became deprecated :
public final Mono<T> retryWhen(Function<Flux<Throwable>, ? extends Publisher<?>> whenFactory)
After hours of googling I haven't found any solution to this question : Is there any alternative for retryWhen
and Retry.onlyIf
with the latest versions of reactor
Thanks for your help !
Solution
Retry used to essentially be a utility function generator distributed as part of reactor-extra
. The API has been altered a bit now and brought into reactor-core
(reactor.util.retry.Retry
), with the old retryWhen()
variant deprecated. So no need to include extra anymore - in your case, you can do something like:
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(10))
.filter(e -> e instanceof ConnectTimeoutException))
Answered By - Michael Berry
Answer Checked By - Clifford M. (JavaFixing Volunteer)