CompletableFuture's sometimes use wrong response from downstream service

25 Views Asked by At

I have this method in my Java application:

public void addPrices(
      Response Response,
      String startDate,
      IContext context)
      throws ServiceException {

    CompletableFuture<PayInfoFull> futurePrimary =
        futurePrimary(response, startDate, context, new PartMapImpl());
    CompletableFuture<PayInfoFull> futureSecondary =
        futureSecondary(response, startDate, context, new PayMapImpl());
    CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(futurePrimary, futureSecondary);

    try {
      combinedFuture.get();
      PayInfoFull primaryPrice = futurePrimary.get();
      PayInfoFull secondaryPrice = futureSecondary.get();
      if (validatePrices(primaryPrice, secondaryPrice)) {
        populateDefaultresponse(response, primaryPrice, secondaryPrice);
      } else {
        response.getPrimary().setPrimaryPrice(null);
        response.getSecondary().setSecndaryPrice(null);
      }

    } catch (InterruptedException | ExecutionException e) {
      LOGGER.error(String.format("Exception: - Exception: %s", e), e);
      throw new ServiceException(STATUS_DOWN);
    }
}

primaryPrice and secondaryPrice make the same call to a downstream service which returns the prices. primaryPrice should always be higher. However sometimes both will have the same price. I can check the messages that the downstream service returns and see that they are always correct but it's as if the CompletableFuture's in this method only see one response and use it for both. How can I investigate this further and hopefully fix the issue?

0

There are 0 best solutions below