Looking for an alternative of retryWhen which is now Deprecated
Asked Answered
M

2

14

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 !

Mania answered 16/6, 2020 at 23:30 Comment(0)
S
15

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))
Stearoptene answered 17/6, 2020 at 20:41 Comment(3)
Thanks a lot, this is excatly what I looked for :) And thanks for the explanation:DMania
cant find Retry.fixedDelay in reactor-extra 3.4.1...Corotto
@Corotto As in the answer - the class you need is now in reactor-core, not reactor-extra.Stearoptene
K
1

Adding only withThrowable to your existing code can make it work. This has worked for me. You can try something like this :

For example :

.retryWhen(withThrowable(Retry.any()
        .doOnRetry(e -> log
            .debug("Retrying to data for {} due to exception: {}", employeeId, e.exception().getMessage()))
        .retryMax(config.getServices().getRetryAttempts())
        .backoff(Backoff.fixed(Duration.ofSeconds(config.getServices().getRetryBackoffSeconds())))))
Kila answered 26/9, 2022 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.