java - 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()
方法的情况下重构上面的代码?
解决方案
推荐阅读
- php - 我可以在mac上的textedit中打开一个php文件吗
- java - 如何在 Elasticsearch 中创建层次结构
- javascript - 切换屏幕时不显示来自 asyncStorage 的项目
- java - Spring Hibernate 没有将实体映射到 PostGres 中的现有表
- python - 是否可以抑制 mypy 的“预期 TypedDict 键为字符串文字”警告?
- python - for循环遍历索引len?
- python-3.x - 如何为我的回测员实施美元追踪止损?
- ios - UIbutton忘记宽度和高度
- python - 如何检查值是否在数据框或 numpy 数组中的两个连续行之间?
- reactjs - 对象无效,因为 React 子代使用数组