How to make multiple Spring Webclient calls in parallel and wait for the result?

Answer #1 100 %

Here is a use case for a parallel call.

public Mono fetchCarrierUserInfo(User user) {
        Mono userInfoMono = fetchUserInfo(user.getGuid());
        Mono carrierInfoMono = fetchCarrierInfo(user.getCarrierGuid());

        return Mono.zip(userInfoMono, carrierInfoMono).map(tuple -> {
            UserInfo userInfo = tuple.getT1();
            userInfo.setCarrier(tuple.getT2());
            return userInfo;
        });
    }

Here:

  • fetchUserInfo makes http call to get user info from another service and returns Mono
  • fetchCarrierInfo method makes HTTP call to get carrierInfo from another service and returns Mono
  • Mono.zip() merges given monos into a new Mono that will be fulfilled when all of the given Monos have produced an item, aggregating their values into a Tuple2.

Then, call fetchCarrierUserInfo().block() it to get the final result.

You’ll also like:


© 2023 CodeForDev.com -