Set timeout per call with CompletableFuture chain?

22 Views Asked by At

Say I have these CFs:

// Get Movies playing for the selected showtime
CompletableFuture<List<Movie>> getMovieList(String day){
        return  CompletableFuture.supplyAsync( ()-> {
            apiCallToSomeService();
    });
}
// Select seats for the movie
CompletableFuture<ShowDetails> selectSeats (ShowTime showTime) {
      return CompletableFuture.supplyAsync(() -> {     
        apiCallToSomeService();
    });
 }
//Calculate ticket price
CompletableFuture<TicketPrice> getTicketPrice (ShowDetails showdetails){
      return CompletableFuture.supplyAsync(() -> {
          apiCallToSomeService();
      });
}

which form the following chain:

    public ShowDetails bookMyShow(ShowDetails showDetails, String promoCode) {
return getMovieList(showDetails.getShowTime().getDay())
            .thenCompose(movie -> selectSeats(showDetails.getShowTime())
            .thenCompose(showDetails2 -> getTicketPrice(showDetails2)))
            .thenApply(output -> doSomeResultMapping(output));
}

Question: how to set timeouts for each API call in this case?

If I just add in the end something like orTimeout(TIMEOUT_PER_API_CALL, TimeUnit.MILLISECONDS) - will it timeout only if all 3 API calls take longer than set in TIMEOUT_PER_API_CALL?

0

There are 0 best solutions below