How to await when all Disposable elements will be finished?

此生再无相见时 提交于 2019-12-25 01:37:23

问题


Lets consider following code:

List<Mono<String>> monoList= apiCall();
List<Disposable> disposableList = monoList.stream()
        .map(m-> m.subscribe(str-> {
                           log.info("Mono is finished with "+ str);
                        })
        ).collect(Collectors.toList());
// I need to await here

I need to await when all mono will be finished.

How could I achieve it?


回答1:


Not mixing different streaming APIs you could utilize side effects instead of subscriptions and await completion with then()

Mono<Void> await = Flux
            .fromIterable(apiCall())
            .flatMap(m -> m.doOnSuccess(
                str -> log.info("Mono is finished with "+ str)
            )).then()


来源:https://stackoverflow.com/questions/58914310/how-to-await-when-all-disposable-elements-will-be-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!