I have following scenario to implement. Get the list of jobs by user Id, iterate jobs and then get account entry by attribute and finally update to account.
Method definition
Mono<Void> execute(List<String> users)
Jobs Dao returning Mono<List>
Mono<List<JobInfo>> getJobsByUser(String user)
class JobInfo {
String user;
String account;
}
Getting account is returning Mono list,
Mono<List<Account>> getAccounts(String account)
I'm struggling to do this using Mono no blocking way. I tried to do this using the block but getting
block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-
public Mono<Void> execute(List<String> users) {
users.forEach(user -> {
jobDao.getJobsByUser(user).block().stream().forEach(job -> {
accountDam.getAccounts(job.getAccount()).block().forEach(acc -> {
// update account
});
});
});
return Mono.empty().then();
}
CodePudding user response:
As far as I can understand, you need the following flow:
public Mono<Void> execute(List<String> users) {
return Flux.fromIterable(users)
.flatMap(user -> getJobsByUser(user))
.flatMapIterable(jobs -> jobs)
.flatMap(job -> accountDam.getAccounts(job.getAccount()))
.flatMapIterable(accounts -> accounts)
.flatMap(account -> updateAccount(account))
.then();
}