Reputation: 606
I'm trying to make parallel (batch) calls to the same rest service using the new SpringBoot 2 Reactive WebClient class(it does not have a batch endpoint). For example I need 100 "Comment" object (with ids 1...100) and I'm doing the following parallel calls :
List<Mono<Comment>> monos = ids.stream()
.map(id -> webClient.get()
.uri("/comments/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Comment.class))
.collect(Collectors.toList());
return Flux.merge(monos);
I'm new to Spring WebFlux and I'm not sure this is the right way of doing parallel calls with the WebClient
Is there a better(more appropriate) way of doing this (i.e. doing a Flux concat of Monos) ?
Also, when I do this the old deprecated AsyncRestTemplate I use a
ThreadPoolExecutor... Should I used a similar concept with the
WebClient ? ... Is there something similar with reactive ?
Regards
The full source code can be bound at : https://github.com/fdlessard/SpringBootReactiveComment
Upvotes: 4
Views: 7688
Reputation: 874
Flux.fromIterable(ids)
.flatMap(id -> webClient.get()
.uri("/comments/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Comment.class))
.subscribeOn(Schedulers.parallel());
Upvotes: 2