Reputation: 3
I want to use WebClient in Spring WebFlux to call some urls, and then put all the monos to flux. when I call Flux.blockLast, I can not get the results.
@Test
public void reactiveGetTest() {
long start = System.currentTimeMillis();
List<String> results = new ArrayList<>();
List<Mono<String>> monos = IntStream.range(0, 500)
.boxed()
.map(i -> reactiveGet("https://www.google.com/"))
.collect(Collectors.toList());
Flux.mergeSequential(monos)
.map(results::add)
.blockLast();
System.out.println("result: " + results.size());
System.out.println("total time: " + (System.currentTimeMillis() - start));
}
private Mono<String> reactiveGet(String url) {
return WebClient.create(url)
.get()
.retrieve()
.bodyToMono(String.class);
}
I want to get a list of size 500, but was 0!
Upvotes: 0
Views: 2257
Reputation: 4410
You can use Flux.collectList()
to get all results in a list:
@Test
public void reactiveGetTest() {
long start = System.currentTimeMillis();
List<Mono<String>> monos = IntStream.range(0, 500)
.boxed()
.map(i -> reactiveGet("https://www.google.com/"))
.collect(Collectors.toList());
List<String> results = Flux.mergeSequential(monos).collectList().block();
System.out.println("result: " + results.size());
System.out.println("total time: " + (System.currentTimeMillis() - start));
}
Upvotes: 1