I'm new with webflux and I'm trying to execute multiple monos with Flux
. But i think i'm doing it wrong.. is this the best approach to execute multiple Mono
and collect it to list?
Here is my code:
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}
}
so far my with above code i can explain it like this:
- Get all branches
- iterate through all
branchesList2
and add it totrxMonoList
- return
Flux.concat
, this is where i'm not sure is this the right way or not. but it's working - combine all list
I'm just confuse is this the proper way to use Flux
in my context? or is there any better way to achieve with what i'm trying to do?