Is it correct to convert CompletableFuture <Stream <T>> to Publisher <T>?
To allow multiple iterations in the resulting stream from a CompletableFuture<Stream<String>>, I consider one of the following approaches:
Transform the resulting future into
CompletableFuture<List<String>>:teams.thenApply(st -> st.collect(toList()))Convert the resulting future to
Flux<String>with cache:Flux.fromStream(teams::join).cache();
Flux<T>represents an implementation Publisher<T>in a design reactor.
Use Case:
I would like to get a sequence with the names of the Premier League teams (e.g. Stream<String>) from a data source that provides an object Leaguewith Standing[](based on the RESTful API for football data, e.g. http://api.football-data.org/v1/soccerseasons/445 / leagueTable ). Using AsyncHttpClientand Gson, we have:
CompletableFuture<Stream<String>> teams = asyncHttpClient
.prepareGet("http://api.football-data.org/v1/soccerseasons/445/leagueTable")
.execute()
.toCompletableFuture()
.thenApply(Response::getResponseBody)
.thenApply(body -> gson.fromJson(body, League.class));
.thenApply(l -> stream(l.standings).map(s -> s.teamName));
, :
1. CompletableFuture<List<String>> res = teams.thenApply(st -> st.collect(toList()))
2. Flux<String> res = Flux.fromStream(teams::join).cache()
Flux<T> , . , ?
CompletableFuture<List<String>> ? ?
(2018-03-16):
CompletableFuture<List<String>>:
- [PROS]
List<String>, , , . - [CONS] .
- [CONS] ,
List<T>.
Flux<String>:
- [PROS]
- [PROS] ,
.cache(), API, . , .@GetMapping(produces =MediaType.TEXT_EVENT_STREAM) public Flux<String> getTeams() {…} - [CONS]
Flux<T>,Flux<T>(….cache()), , , , .