How to convert Mono<List<String>> into Flux<String>

坚强是说给别人听的谎言 提交于 2019-11-28 02:36:13

问题


I'm converting small project written in RxJava 1.x to Reactor 3.x. All is good, except that I could not find out how to replace flatMap(Observable::from) with appropriate counterpart. I have Mono<List<String>> and I need to convert it to Flux<String>.

Thanks


回答1:


In Reactor 3, the from operator has been specialized into a few variants, depending on the original source (array, iterable, etc...).

Use yourMono.flatMapMany(Flux::fromIterable) in your case.




回答2:


I think that probably Flux::mergeSequential static factory fits better here:

 Iterable<Mono<String>> monos = ...
 Flux<String> f = Flux.mergeSequential(monos);

This kind of merge (sequential) will maintain the ordering inside given source iterable, and will also subscribe/request eagerly from all participating sources (so more parallelization expected while computing mono results).




回答3:


Thanks Simon, I implemented something like this:

List<Object> dbObjects = ListObjectsBD();
    List<Dao> daos = mapperObjToDao(dbObjects);
    Flux<Dao> daoFlux = Mono.just(daos).flatMapMany(Flux::fromIterable);


来源:https://stackoverflow.com/questions/42007841/how-to-convert-monoliststring-into-fluxstring

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