How to use Spring WebClient to make multiple calls simultaneously?
You can make HTTP calls concurrently using simple RestTemplate
and ExecutorService
:
RestTemplate restTemplate = new RestTemplate();
ExecutorService executorService = Executors.newCachedThreadPool();
Future<String> firstCallFuture = executorService.submit(() -> restTemplate.getForObject("http://first-call-example.com", String.class));
Future<String> secondCallFuture = executorService.submit(() -> restTemplate.getForObject("http://second-call-example.com", String.class));
String firstResponse = firstCallFuture.get();
String secondResponse = secondCallFuture.get();
executorService.shutdown();
Or
Future<String> firstCallFuture = CompletableFuture.supplyAsync(() -> restTemplate.getForObject("http://first-call-example.com", String.class));
Future<String> secondCallFuture = CompletableFuture.supplyAsync(() -> restTemplate.getForObject("http://second-call-example.com", String.class));
String firstResponse = firstCallFuture.get();
String secondResponse = secondCallFuture.get();
You can use Spring reactive client WebClient
to send parallel requests.
In this example,
public Mono<UserInfo> getUserInfo(User user) {
Mono<UserInfo> userInfoMono = getUserInfo(user.getId());
Mono<OrgInfo> organizationInfoMono = getOrgInfo(user.getOrgId());
return Mono.zip(userInfoMono, organizationInfoMono).map(tuple -> {
UserInfo userInfo = tuple.getT1();
userInfo.setOrganization(tuple.getT2());
return userInfo;
});
}
Here:
getUserInfo
makes an HTTP call to get user info from another service and returnsMono
getOrgInfo
method makes HTTP call to get organization info from another service and returnsMono
Mono.zip()
waits all the results from all monos and merges into a new mono and returns it.
Then, call getOrgUserInfo().block()
to get the final result.
Assuming a WebClient wrapper (like in reference doc):
@Service
public class MyService {
private final WebClient webClient;
public MyService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.org").build();
}
public Mono<Details> someRestCall(String name) {
return this.webClient.get().url("/{name}/details", name)
.retrieve().bodyToMono(Details.class);
}
}
..., you could invoke it asynchronously via:
// ...
@Autowired
MyService myService
// ...
Mono<Details> foo = myService.someRestCall("foo");
Mono<Details> bar = myService.someRestCall("bar");
Mono<Details> baz = myService.someRestCall("baz");
// ..and use the results (thx to: [2] & [3]!):
// Subscribes sequentially:
// System.out.println("=== Flux.concat(foo, bar, baz) ===");
// Flux.concat(foo, bar, baz).subscribe(System.out::print);
// System.out.println("\n=== combine the value of foo then bar then baz ===");
// foo.concatWith(bar).concatWith(baz).subscribe(System.out::print);
// ----------------------------------------------------------------------
// Subscribe eagerly (& simultaneously):
System.out.println("\n=== Flux.merge(foo, bar, baz) ===");
Flux.merge(foo, bar, baz).subscribe(System.out::print);
Mono javadoc
Flux javadoc
Spring WebClient reference doc
Spring Boot WebClient reference doc
Projectreactor reference doc
Which (reactive) operator to use!
Thanks, Welcome & Kind Regards,