首页 > 解决方案 > Spring Webflux 在一个 Flux 中等待一个 Flux

问题描述

这是场景,我有一个来自 ReactiveMongoDB 和另一个来自 WebClient 的 Flux,所以我需要对于 Mongodb 中的每个项目,我在 webclient 中查找他的项目。

问题是,我一直在使用块来等待 webclient 的到来,这会影响性能。

如果我不使用 Block,则在没有来自 webClient 的 itens 的情况下发送响应,所以它可能不会等待这个 itens 来。

有什么方法可以拨打所有电话并稍后再等待?

 return planetaRepository.findAll().flatMap(planetaVO -> {
                planetaServiceFacade.recuperarFilmesParticipados(planetaVO.getNome()).collectList().doOnNext(planetaVO::setFilmes).block();
                return Flux.just(planetaVO);
            });

标签: javaspringspring-webfluxproject-reactorspring-mongodb

解决方案


你可以尝试这样的事情,通过使用thenReturn

return planetaRepository.findAll().flatMap(planetaVO -> {
    return planetaServiceFacade.recuperarFilmesParticipados(planetaVO.getNome()).collectList()
                        .doOnNext(planetaVO::setFilmes)
                        .thenReturn(planetaVO);
});

如果你不是被迫的话,我建议你不要用你的母语编写代码。


推荐阅读