寻找 retryWhen 的替代方案,现在已弃用

Pie*_*nes 6 reactive-programming spring-boot project-reactor reactor-netty spring-webclient

我正面临与WebClient和的问题reactor-extra。确实,我有以下方法:

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();
    }
Run Code Online (Sandbox Code Playgroud)

我发现我可以使用,retryWhen(Retry.onlyIf(...))因为我只想在ConnectTimeoutException抛出a 时重试。我从这篇文章中找到了这个解决方案:spring webclient: retry with backoff on specific error

但是,在reactor以下方法的最新版本中已弃用:

public final Mono<T> retryWhen(Function<Flux<Throwable>, ? extends Publisher<?>> whenFactory)

谷歌搜索后,我还没有发现任何解决这个问题的时间:是否有任何替代retryWhenRetry.onlyIf使用的最新版本reactor

谢谢你的帮助 !

Mic*_*rry 9

重试曾经本质上是一个效用函数生成器,作为reactor-extra. 现在 API 已稍作改动并带入reactor-core( reactor.util.retry.Retry) 中,旧retryWhen()版本已弃用。因此,无需再包含额外内容 - 在您的情况下,您可以执行以下操作:

.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(10))
        .filter(e -> e instanceof ConnectTimeoutException))
Run Code Online (Sandbox Code Playgroud)