From 1d4a7681e2d8699457c557fbab3d69d5f8aa105b Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Fri, 1 Nov 2019 23:31:30 +0530 Subject: [PATCH] Fix for Issue #549 : Add Fallbacks in Aggregator Service (#971) * Fix for Issue##549 Catch ClientProtocolException and Update Error Logs * Fix indentation, checkstyle errors * Fix for Issue #549 Add fallbacks in Aggregator service when other microservices fail * Make ProductInventoryClientImpl return null instead of zero in case of failure --- .../aggregator/microservices/Aggregator.java | 18 ++++++++++++++++-- .../microservices/ProductInventoryClient.java | 2 +- .../ProductInventoryClientImpl.java | 10 +++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java index e96ac9d43..dbed50fc6 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java @@ -51,9 +51,23 @@ public class Aggregator { */ @RequestMapping(path = "/product", method = RequestMethod.GET) public Product getProduct() { + var product = new Product(); - product.setTitle(informationClient.getProductTitle()); - product.setProductInventories(inventoryClient.getProductInventories()); + String productTitle = informationClient.getProductTitle(); + Integer productInventory = inventoryClient.getProductInventories(); + + if (productTitle != null) { + product.setTitle(productTitle); + } else { + product.setTitle("Error: Fetching Product Title Failed"); //Fallback to error message + } + + if (productInventory != null) { + product.setProductInventories(productInventory); + } else { + product.setProductInventories(-1); //Fallback to default error inventory + } + return product; } diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java index ed325be00..22369350a 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java @@ -28,5 +28,5 @@ package com.iluwatar.aggregator.microservices; */ public interface ProductInventoryClient { - int getProductInventories(); + Integer getProductInventories(); } diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java index c43fe84c6..fcd824de7 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java @@ -42,8 +42,8 @@ public class ProductInventoryClientImpl implements ProductInventoryClient { private static final Logger LOGGER = LoggerFactory.getLogger(ProductInventoryClientImpl.class); @Override - public int getProductInventories() { - var response = "0"; + public Integer getProductInventories() { + var response = ""; var request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories")).build(); var client = HttpClient.newHttpClient(); @@ -55,6 +55,10 @@ public class ProductInventoryClientImpl implements ProductInventoryClient { } catch (InterruptedException ie) { LOGGER.error("InterruptedException Occurred", ie); } - return Integer.parseInt(response); + if("".equalsIgnoreCase(response)) { + return null; + } else { + return Integer.parseInt(response); + } } }