【发布时间】:2020-08-12 16:09:27
【问题描述】:
我是 webflux 的新手,我正在尝试使用 Flux 执行多个单声道。但我认为我做错了.. 这是执行多个Mono 并将其收集到列表的最佳方法吗?
这是我的代码:
mainService.getAllBranch()
.flatMapMany(branchesList -> {
List<Branch> branchesList2 = (List<Branch>) branchesList.getData();
List<Mono<Transaction>> trxMonoList= new ArrayList<>();
branchesList2.stream().forEach(branch -> {
trxMonoList.add(mainService.getAllTrxByBranchId(branch.branchId));
});
return Flux.concat(trxMonoList); // <--- is there any other way than using concat?
})
.collectList()
.flatMap(resultList -> combineAllList());
interface MainService{
Mono<RespBody> getAllBranch();
Mono<RespBody> getAllTrxByBranchId(String branchId); //will return executed url ex: http://trx.com/{branchId}
}
到目前为止,我可以这样解释:
- 获取所有分支
- 遍历所有
branchesList2并将其添加到trxMonoList - 返回
Flux.concat,这是我不确定这是否正确的地方。但它正在工作 - 合并所有列表
我只是困惑这是在我的上下文中使用Flux 的正确方法吗?还是有更好的方法来实现我正在尝试做的事情?
【问题讨论】:
标签: java spring-boot spring-webflux project-reactor