简体   繁体   中英

Java Reactive. How to wait for all data in flux and then process them

I'm getting data from mongo reactive repository and updating it. Then I have to collect all data in one collection and path it to another service to get more info. Then I should map them in one Flux. My code is:

        Flux<Views> views = someRepository.findAllByUsersIn(userId).doOnNext(v -> {
        v.setInterlocutor(v.getUsers().stream().filter(u -> !userId.equals(u)).findFirst().orElse(null));
    });
    

    return Flux.zip(views.map(view -> conversionService.convert(view, ResponseViewDto.class)), getUserInfo(views).flux())
            .flatMap(fZip -> {
                ResponseViewDto dto = fZip.getT1();
                dto.setInterlocutor(fZip.getT2().get(dto.getInter()));
                return Flux.just(dto);
            });

getUserInfo does collecting usersId and sends them to another service and returns expanded info.

I found that getting from DB calls 2 times and I can understand why, but is there any solution to do it once and still be not blocking.

Thanks to Adhika Setya Pramudita for help. The way to do what I need is just to use cache() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM