Search code examples
javareactive-programmingspring-webflux

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.


Solution

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