Decorate and execute a functional interface ... TimeLimiter has higher order decorator functions to decorate a CompletionStage or. Future in order to limit the ...
xxxxxxxxxx
@Service
public class RatingServiceClient {
private final RestTemplate restTemplate = new RestTemplate();
@Value("${rating.service.endpoint}")
private String ratingService;
@TimeLimiter(name = "ratingService", fallbackMethod = "getDefault")
public CompletionStage<ProductRatingDto> getProductRatingDto(int productId){
Supplier<ProductRatingDto> supplier = () ->
this.restTemplate.getForEntity(this.ratingService + productId, ProductRatingDto.class)
.getBody();
return CompletableFuture.supplyAsync(supplier);
}
private CompletionStage<ProductRatingDto> getDefault(int productId, Throwable throwable){
return CompletableFuture.supplyAsync(() -> ProductRatingDto.of(0, Collections.emptyList()));
}
}