我如何等待 List<Mono> 得到解决?

我希望能够等待 Monos 列表得到解决,并在完成最后一个元素后,继续进行另一个异步调用


public Mono<Artist> getArtistInfo(String id) {

    //Call API1

    Mono<MusicResponse> musisResponseMono = webClientBuilder

                                                .build()

                                                .get()

                                                .uri(uri + "\\" + id)

                                                .retrieve()

                                                .bodyToMono(MusicResponse.class);


    //1.async handler for the first call

    return musisResponseMono.flatMap(musicRes ->{


        Artist artist = new Artist();


        List<Albums> albums = musicRes.getAlbums();


        //make mutiple concurrent API calls to API2

        albums.stream().forEach( album -> {

            webClientBuilder

                .build()

                .get().uri("API 2 URL")

                .retrieve()

                .bodyToMono(Covers.class)

                .subscribe(cover -> artist.getAlbums().add(cover.getImage()));

        });



        //call API3 - want to wait untill all API calls 2 are completed

        return webClientBuilder

            .build()

            .get()

            .uri("API3 URL")

            .retrieve()

            .bodyToMono(Profiles.class)

            .map( profileRes ->

                artist.setDescription(profileRes.getDescription())

            );



    }

    

}

问题是 API 调用 3 可能在返回第二个调用的每个元素之前返回。我想我正在寻找的是 Spring Webflux 上下文中 Javascipt 中的 asycn await 之类的东西


慕容森
浏览 108回答 1
1回答

哔哔one

我认为这应该有效。我们首先获取艺术家并映射响应。在映射期间,我们获取每个专辑封面并获得一个List<Mono<Cover>>块。然后我们将它们合并到一个中,当它们可用时Flux#merge将发出每个。Cover所以我们可以在每个上发出doOnNext并将它们附加到艺术家。完成后,我们只需then返回艺术家对象。在这些事件链之后,我们可以doOnSuccess第二次获取并将更多信息附加到Artist对象。public Mono<Artist> getArtistInfo(String id) {&nbsp; &nbsp; return webClient.get()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .uri("/artist")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .retrieve()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .bodyToMono(ArtistResponse.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(artistResponse -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Artist artist = new Artist();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Flux.fromIterable(artistResponse.getAlbums())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .parallel(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .runOn(Schedulers.parallel())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(albums -> webClient.get()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .uri("artist/albums")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .retrieve()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .bodyToMono(Covers.class))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .doOnNext(coversMono -> coversMono&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .doOnSuccess(covers -> artist.getAlbums().add(covers)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .thenReturn(artist);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .doOnSuccess(artist -> webClient.get()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .uri("/artist/profile")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .retrieve()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .bodyToMono(Profiles.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .doOnSuccess(profiles -> artist.setDescription(profiles.getDescription())));}没有运行代码,所以不能保证它,但至少它会给你一些洞察力,并在路上迈出一步。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java