Skip to content

Commit 1d4a768

Browse files
jarpit96iluwatar
authored andcommitted
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
1 parent 55b0341 commit 1d4a768

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/Aggregator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,23 @@ public class Aggregator {
5151
*/
5252
@RequestMapping(path = "/product", method = RequestMethod.GET)
5353
public Product getProduct() {
54+
5455
var product = new Product();
55-
product.setTitle(informationClient.getProductTitle());
56-
product.setProductInventories(inventoryClient.getProductInventories());
56+
String productTitle = informationClient.getProductTitle();
57+
Integer productInventory = inventoryClient.getProductInventories();
58+
59+
if (productTitle != null) {
60+
product.setTitle(productTitle);
61+
} else {
62+
product.setTitle("Error: Fetching Product Title Failed"); //Fallback to error message
63+
}
64+
65+
if (productInventory != null) {
66+
product.setProductInventories(productInventory);
67+
} else {
68+
product.setProductInventories(-1); //Fallback to default error inventory
69+
}
70+
5771
return product;
5872
}
5973

aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
*/
2929
public interface ProductInventoryClient {
3030

31-
int getProductInventories();
31+
Integer getProductInventories();
3232
}

aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public class ProductInventoryClientImpl implements ProductInventoryClient {
4242
private static final Logger LOGGER = LoggerFactory.getLogger(ProductInventoryClientImpl.class);
4343

4444
@Override
45-
public int getProductInventories() {
46-
var response = "0";
45+
public Integer getProductInventories() {
46+
var response = "";
4747

4848
var request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories")).build();
4949
var client = HttpClient.newHttpClient();
@@ -55,6 +55,10 @@ public int getProductInventories() {
5555
} catch (InterruptedException ie) {
5656
LOGGER.error("InterruptedException Occurred", ie);
5757
}
58-
return Integer.parseInt(response);
58+
if("".equalsIgnoreCase(response)) {
59+
return null;
60+
} else {
61+
return Integer.parseInt(response);
62+
}
5963
}
6064
}

0 commit comments

Comments
 (0)