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
This commit is contained in:
Arpit Jain 2019-11-01 23:31:30 +05:30 committed by Ilkka Seppälä
parent 55b0341c8d
commit 1d4a7681e2
3 changed files with 24 additions and 6 deletions

View File

@ -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;
}

View File

@ -28,5 +28,5 @@ package com.iluwatar.aggregator.microservices;
*/
public interface ProductInventoryClient {
int getProductInventories();
Integer getProductInventories();
}

View File

@ -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);
}
}
}