助焊剂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()方法的情况下重构上述代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)