|
| 1 | +package com.bootcamptoprod.retry.service; |
| 2 | + |
| 3 | +import com.bootcamptoprod.retry.entity.Movie; |
| 4 | +import com.bootcamptoprod.retry.rest.client.MovieApiClient; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.retry.RetryCallback; |
| 7 | +import org.springframework.retry.support.RetryTemplate; |
| 8 | +import org.springframework.stereotype.Service; |
| 9 | +import org.springframework.web.client.HttpClientErrorException; |
| 10 | +import org.springframework.web.client.HttpServerErrorException; |
| 11 | +import org.springframework.web.client.ResourceAccessException; |
| 12 | +import org.springframework.web.client.RestClientException; |
| 13 | + |
| 14 | +@Service |
| 15 | +public class MovieService { |
| 16 | + |
| 17 | + @Autowired |
| 18 | + private MovieApiClient movieApiClient; |
| 19 | + |
| 20 | + @Autowired |
| 21 | + private RetryTemplate retryTemplate; |
| 22 | + |
| 23 | + public Movie getMovieDetails(String movieId) { |
| 24 | + return retryTemplate.execute((RetryCallback<Movie, RestClientException>) context -> { |
| 25 | + Movie movie = null; |
| 26 | + try { |
| 27 | + movie = movieApiClient.getMovieDetails(movieId); |
| 28 | + } catch (HttpServerErrorException httpServerErrorException) { |
| 29 | + System.out.println("Received HTTP server error exception while fetching the movie details. Error Message: " + httpServerErrorException.getMessage()); |
| 30 | + throw httpServerErrorException; |
| 31 | + } catch (HttpClientErrorException httpClientErrorException) { |
| 32 | + System.out.println("Received HTTP client error exception while fetching the movie details. Error Message: " + httpClientErrorException.getMessage()); |
| 33 | + throw httpClientErrorException; |
| 34 | + } catch (ResourceAccessException resourceAccessException) { |
| 35 | + System.out.println("Received Resource Access exception while fetching the movie details."); |
| 36 | + throw resourceAccessException; |
| 37 | + } |
| 38 | + return movie; |
| 39 | + }); |
| 40 | + } |
| 41 | +} |
0 commit comments