首页 > 解决方案 > Flux next() 导致性能不佳

问题描述

我是响应式编程的新手,我想在我们现有的项目上实现 spring-webclient。

为简单起见,我根据我的原始代码创建了一个伪代码。在这里,我向每个提供商发送短信。如果满足条件,则不会进入下一个提供者。

public List<Sms> send(List<Sms> smsRequests) {
    return Flux.fromIterable(smsRequests)
            .flatMap(smsRequest -> {
                return Flux.fromIterable(smsRequest.getProviders())
                        .concatMap(smsProvider -> this.invokeApiUsingWebClient(smsProvider, smsRequest))
                        .filter(providerResponse -> providerResponse.getErrorStatus() == null)
                        .map(ProviderResponse::toSms)
                        .next(); // emits cancel(). Resulting to connection being closed.
            })
            .toStream()
            .collect(Collectors.toList());
}

cancel()我的问题是每次调用这个流next()调用。由于不重用 webclient 线程上的连接而导致性能不佳。

请参阅下面的日志。

TRACE 15112 [reactor-http-nio-1] [ExchangeFunctions] - [5e2f49c] Response 200 OK...
INFO 15112 [reactor-http-nio-1] [1] - onNext(...)
INFO 15112 [reactor-http-nio-1] [1] - cancel()
DEBUG 15112 [reactor-http-nio-1] [ExchangeFunctions] - [5e2f49c] Cancel signal (to close connection)
INFO 15112 [reactor-http-nio-1] [1] - onComplete()

TRACE 15112 [reactor-http-nio-2] [ExchangeFunctions] - [5e2f49c] Response 200 OK...
INFO 15112 [reactor-http-nio-2] [2] - onNext(...)
INFO 15112 [reactor-http-nio-2] [2] - cancel()
DEBUG 15112 [reactor-http-nio-2] [ExchangeFunctions] - [5e2f49c] Cancel signal (to close connection)
INFO 15112 [reactor-http-nio-2] [2] - onComplete()

有没有可能在不发出cancel()方法的情况下重构上面的代码?

标签: javaspring-webfluxproject-reactorspring-webclient

解决方案


推荐阅读