|
| 1 | +package org.example.apiclient; |
| 2 | + |
| 3 | +import org.example.domain.movie.Movie; |
| 4 | +import org.example.domain.movie.MovieInfo; |
| 5 | +import org.example.domain.movie.Review; |
| 6 | +import org.springframework.web.reactive.function.client.WebClient; |
| 7 | +import org.springframework.web.util.UriComponentsBuilder; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author milad mofidi |
| 15 | + |
| 16 | + * user: miladm on 2/18/2023 |
| 17 | + */ |
| 18 | +public class MoviesClient |
| 19 | +{ |
| 20 | + private final WebClient webClient; |
| 21 | + |
| 22 | + public MoviesClient(WebClient webClient) |
| 23 | + { |
| 24 | + this.webClient = webClient; |
| 25 | + } |
| 26 | + //Implementing in blocking way |
| 27 | + public Movie retrieveMovie(Long movieInfoId){ |
| 28 | + |
| 29 | + var movieInfo = invokeMovieInfo(movieInfoId); |
| 30 | + var reviews =invokeReviewsService(movieInfoId); |
| 31 | + return new Movie(movieInfo, reviews); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + //Implementing in non-blocking way |
| 36 | + public CompletableFuture<Movie> retrieveMovie_Cf_async(Long movieInfoId){ |
| 37 | + |
| 38 | + var movieInfo = CompletableFuture.supplyAsync( () ->invokeMovieInfo(movieInfoId)) ; |
| 39 | + var reviews =CompletableFuture.supplyAsync( () ->invokeReviewsService(movieInfoId)) ; |
| 40 | + return movieInfo |
| 41 | + .thenCombine(reviews, (movieInfoResult, reviewsResult) ->{ |
| 42 | + return new Movie(movieInfoResult, reviewsResult); |
| 43 | + }); |
| 44 | + |
| 45 | + } |
| 46 | + //Calling traditional retrieve movie method to make list of loaded movies |
| 47 | + public List<Movie> retrieveMovies(List<Long> movieInfoIds){ |
| 48 | + return movieInfoIds.stream().map(movieInfoId -> retrieveMovie(movieInfoId)).collect(Collectors.toList()); |
| 49 | + } |
| 50 | + |
| 51 | + //Calling completable-future retrieve movie method to make list of loaded movies using retrieveMovie_Cf_async() method |
| 52 | + public List<Movie> retrieveMovieList_Cf_async(List<Long> movieInfoIds){ |
| 53 | + var movieFutures = movieInfoIds.stream().map(this::retrieveMovie_Cf_async). |
| 54 | + collect(Collectors.toList()); |
| 55 | + return movieFutures. |
| 56 | + stream(). |
| 57 | + map(CompletableFuture::join) |
| 58 | + .collect(Collectors.toList()); |
| 59 | + } |
| 60 | + |
| 61 | + //Using "allOf()" method to improve performance, this method will wait until all items get processed |
| 62 | + public List<Movie> retrieveMovieList_Cf_async_allOf(List<Long> movieInfoIds){ |
| 63 | + var movieFutures = movieInfoIds.stream().map(this::retrieveMovie_Cf_async). |
| 64 | + collect(Collectors.toList()); |
| 65 | + |
| 66 | + var cfAllOf = CompletableFuture.allOf(movieFutures.toArray(new CompletableFuture[movieFutures.size()])); |
| 67 | + return cfAllOf.thenApply(v -> movieFutures |
| 68 | + .stream() |
| 69 | + .map(CompletableFuture::join) |
| 70 | + .collect(Collectors.toList())) |
| 71 | + .join(); |
| 72 | + } |
| 73 | + |
| 74 | + //Implementing the web client call in blocking way |
| 75 | + private MovieInfo invokeMovieInfo(Long movieInfoId) |
| 76 | + { |
| 77 | + var movieInfoUrlPath = "/v1/reviews?movieInfoId=2"; |
| 78 | + return webClient.get().uri(movieInfoUrlPath, movieInfoId).retrieve().bodyToMono(MovieInfo.class).block(); |
| 79 | + } |
| 80 | + |
| 81 | + //Implementing the web client call in blocking way |
| 82 | + private List<Review> invokeReviewsService(Long movieInfoId) |
| 83 | + { |
| 84 | + ///v1/reviews?movieInfoId=2 |
| 85 | + var reviewUri = UriComponentsBuilder.fromUriString("/v1/reviews").queryParam("movieInfoId", movieInfoId).buildAndExpand().toString(); |
| 86 | + return webClient.get().uri(reviewUri).retrieve().bodyToFlux(Review.class).collectList().block(); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments