【问题标题】:Looking for an alternative of retryWhen which is now Deprecated寻找现在已弃用的 retryWhen 的替代方案
【发布时间】:2023-03-20 00:23:02
【问题描述】:

我遇到了WebClientreactor-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();
    }

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

但是,在最新版本的reactor 中,不推荐使用以下方法:

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

经过数小时的谷歌搜索,我还没有找到任何解决这个问题的方法:retryWhenRetry.onlyIf 有没有最新版本的reactor 的替代品

感谢您的帮助!

【问题讨论】:

    标签: spring-boot reactive-programming project-reactor spring-webclient reactor-netty


    【解决方案1】:

    Retry 曾经本质上是作为reactor-extra 的一部分分发的实用函数生成器。 API 现在已经进行了一些更改,并被引入reactor-core (reactor.util.retry.Retry),旧的retryWhen() 变体已弃用。因此,无需再包含额外内容 - 在您的情况下,您可以执行以下操作:

    .retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(10))
            .filter(e -> e instanceof ConnectTimeoutException))
    

    【讨论】:

    • 非常感谢,这正是我所寻找的 :) 感谢您的解释:D
    • 在 reactor-extra 3.4.1 中找不到 Retry.fixedDelay...
    • @user1955934 正如答案 - 你需要的课程现在在reactor-core,而不是reactor-extra
    猜你喜欢
    • 2019-03-24
    • 2012-04-16
    • 1970-01-01
    • 2016-01-08
    • 2020-11-07
    • 2020-05-22
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多