From 111e1581581727c43de6149a3855188bd340b60e Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Sun, 6 Oct 2019 01:26:00 +0530 Subject: [PATCH 1/4] Fix for Issue##549 Catch ClientProtocolException and Update Error Logs --- .../microservices/ProductInformationClientImpl.java | 9 ++++++--- .../microservices/ProductInventoryClientImpl.java | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java index 16849d529386..b7904c80ad2d 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java @@ -22,6 +22,7 @@ */ package com.iluwatar.aggregator.microservices; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -49,9 +50,11 @@ public String getProductTitle() { try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { response = EntityUtils.toString(httpResponse.getEntity()); } - } catch (IOException e) { - LOGGER.error("Exception caught.", e); - } + } catch (ClientProtocolException cpe) { + LOGGER.error("ClientProtocolException Occured", cpe); + } catch (IOException ioe) { + LOGGER.error("IOException Occurred", ioe); + } return response; } } 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 89f1a25e0580..0613550abd43 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 @@ -22,6 +22,7 @@ */ package com.iluwatar.aggregator.microservices; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -49,9 +50,11 @@ public int getProductInventories() { try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { response = EntityUtils.toString(httpResponse.getEntity()); } - } catch (IOException e) { - LOGGER.error("Exception caught.", e); - } + } catch (ClientProtocolException cpe) { + LOGGER.error("ClientProtocolException Occured", cpe); + } catch (IOException ioe) { + LOGGER.error("IOException Occurred", ioe); + } return Integer.parseInt(response); } } From d887def66f565c5a33a5da88321ba9c5e223c679 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Sun, 6 Oct 2019 01:39:34 +0530 Subject: [PATCH 2/4] Fix indentation, checkstyle errors --- .../microservices/ProductInformationClientImpl.java | 10 +++++----- .../microservices/ProductInventoryClientImpl.java | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java index b7904c80ad2d..131fbf8695cd 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java @@ -50,11 +50,11 @@ public String getProductTitle() { try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { response = EntityUtils.toString(httpResponse.getEntity()); } - } catch (ClientProtocolException cpe) { - LOGGER.error("ClientProtocolException Occured", cpe); - } catch (IOException ioe) { - LOGGER.error("IOException Occurred", ioe); - } + } catch (ClientProtocolException cpe) { + LOGGER.error("ClientProtocolException Occured", cpe); + } catch (IOException ioe) { + LOGGER.error("IOException Occurred", ioe); + } return response; } } 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 0613550abd43..780dccb78e20 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 @@ -50,11 +50,11 @@ public int getProductInventories() { try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { response = EntityUtils.toString(httpResponse.getEntity()); } - } catch (ClientProtocolException cpe) { - LOGGER.error("ClientProtocolException Occured", cpe); - } catch (IOException ioe) { - LOGGER.error("IOException Occurred", ioe); - } + } catch (ClientProtocolException cpe) { + LOGGER.error("ClientProtocolException Occured", cpe); + } catch (IOException ioe) { + LOGGER.error("IOException Occurred", ioe); + } return Integer.parseInt(response); } } From 606c665b1ef807530cefa59912314b1736c71d19 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Tue, 8 Oct 2019 01:55:07 +0530 Subject: [PATCH 3/4] Fix for Issue #549 Add fallbacks in Aggregator service when other microservices fail --- .../aggregator/microservices/Aggregator.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 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 9f1e4f4daeb8..0151b97eca7c 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 @@ -50,8 +50,21 @@ public class Aggregator { @RequestMapping("/product") public Product getProduct() { Product 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; } From c730b1f3f7ea4951a116175ef9573bfb30f3f6b2 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Fri, 1 Nov 2019 20:58:39 +0530 Subject: [PATCH 4/4] Make ProductInventoryClientImpl return null instead of zero in case of failure --- .../aggregator/microservices/ProductInventoryClient.java | 2 +- .../microservices/ProductInventoryClientImpl.java | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) 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 ed325be0070f..22369350a203 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 @@ */ 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 6ff4a7269868..fcd824de7aca 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,7 +42,7 @@ public class ProductInventoryClientImpl implements ProductInventoryClient { private static final Logger LOGGER = LoggerFactory.getLogger(ProductInventoryClientImpl.class); @Override - public int getProductInventories() { + public Integer getProductInventories() { var response = ""; var request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories")).build(); @@ -55,6 +55,10 @@ public int getProductInventories() { } catch (InterruptedException ie) { LOGGER.error("InterruptedException Occurred", ie); } - return Integer.parseInt(response); + if("".equalsIgnoreCase(response)) { + return null; + } else { + return Integer.parseInt(response); + } } }