Home > Net >  How to use Spring WebClient to make multiple calls simultaneously and get response separately?
How to use Spring WebClient to make multiple calls simultaneously and get response separately?

Time:03-16

I have to execute multiple API calls simultaneously which are independent of each other:

Mono<Response1> response1= this.webClient
               .post()
               .uri(requestURI1)
               .body(Flux.just(request.getRequestBody()), ParameterizedTypeReference<T>)
               .exchangeToMono(response -> {
                   return response.statusCode().equals(HttpStatus.OK)
                            ? response.bodyToMono(ParameterizedTypeReference<T>)
                            : response.createException().flatMap(Mono::error);
                });

Mono<Response2> response2= this.webClient
               .post()
               .uri(requestURI2)
               .body(Flux.just(request.getRequestBody()), ParameterizedTypeReference<T>)
               .exchangeToMono(response -> {
                   return response.statusCode().equals(HttpStatus.OK)
                            ? response.bodyToMono(ParameterizedTypeReference<T>)
                            : response.createException().flatMap(Mono::error);
                });

Mono<Response3> response3= this.webClient
               .post()
               .uri(requestURI3)
               .body(Flux.just(request.getRequestBody()), ParameterizedTypeReference<T>)
               .exchangeToMono(response -> {
                   return response.statusCode().equals(HttpStatus.OK)
                            ? response.bodyToMono(ParameterizedTypeReference<T>)
                            : response.createException().flatMap(Mono::error);
                });

How can I get the response of above api calls in separate Objects and at the same time, they should be executed in parallel? Also, after executing these above calls and putting the data in separate objects, say, Response1, Response2, Response3, I want to execute another API call which consumes these responses Response1, Response2, Response3.

I have tried to use Flux.merge but this will merge the responses in single object which is not correct. Also read about Mono.zip, but these all are used to combine the responses which I don't want.

CodePudding user response:

To combine publishers in parallel you can use Mono.zip that will return TupleX<...> and will be resolved when all publishers are resolved.

Mono<Tuple3<Response1, Response2, Response3>> res = Mono.zip(response1, response2, response3);

Depending on the error handling logic you could consider zipDelayError

CodePudding user response:

Read up on ExecutorService. This is a thread pool which you can use to submit tasks to be executed in separate threads in paralel. You will need to implement the Callable interface and get Future as your results. That gives you desired functionality

  • Related