From 6f4d2e549f69c872af10f43d45dd208b0817f882 Mon Sep 17 00:00:00 2001 From: Chinmoy Chakraborty Date: Mon, 10 Apr 2023 18:30:11 +0530 Subject: [PATCH] Modified health check endpoints to work asynchronously. --- .../uci/outbound/health/HealthController.java | 28 ++++------ .../health/ServiceStatusController.java | 54 +++++++++---------- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/uci/outbound/health/HealthController.java b/src/main/java/com/uci/outbound/health/HealthController.java index 76c12ba..06f3e51 100644 --- a/src/main/java/com/uci/outbound/health/HealthController.java +++ b/src/main/java/com/uci/outbound/health/HealthController.java @@ -1,18 +1,10 @@ package com.uci.outbound.health; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; import com.uci.dao.service.HealthService; - import com.uci.utils.model.ApiResponse; import com.uci.utils.model.ApiResponseParams; import lombok.extern.slf4j.Slf4j; - -import java.io.IOException; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; +import reactor.core.publisher.Mono; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -29,15 +21,13 @@ public class HealthController { private HealthService healthService; @RequestMapping(value = "/health", method = RequestMethod.GET, produces = { "application/json", "text/json" }) - public ResponseEntity statusCheck() throws JsonProcessingException, IOException { - log.error("Health API called"); - ApiResponse response = ApiResponse.builder() - .id("api.health") - .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) - .result(healthService.getAllHealthNode()) - .build(); - - return ResponseEntity.ok(response); + public Mono> statusCheck() { + return healthService.getAllHealthNode().map(health -> ApiResponse.builder() + .id("api.health") + .params(ApiResponseParams.builder().build()) + .responseCode(HttpStatus.OK.name()) + .result(health) + .build() + ).map(ResponseEntity::ok); } } diff --git a/src/main/java/com/uci/outbound/health/ServiceStatusController.java b/src/main/java/com/uci/outbound/health/ServiceStatusController.java index 34789c9..d41d0ec 100644 --- a/src/main/java/com/uci/outbound/health/ServiceStatusController.java +++ b/src/main/java/com/uci/outbound/health/ServiceStatusController.java @@ -10,6 +10,7 @@ import com.uci.utils.model.ApiResponse; import com.uci.utils.model.ApiResponseParams; import lombok.extern.slf4j.Slf4j; +import reactor.core.publisher.Mono; import java.io.IOException; @@ -49,38 +50,37 @@ public ResponseEntity statusCheck() throws JsonProcessingException } @RequestMapping(value = "/health/cassandra", method = RequestMethod.GET, produces = { "application/json", "text/json" }) - public ResponseEntity cassandraStatusCheck() throws IOException, JsonProcessingException { - ApiResponse response = ApiResponse.builder() - .id("api.service.health.cassandra") - .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) - .result(healthService.getCassandraHealthNode()) - .build(); - - return ResponseEntity.ok(response); + public Mono> cassandraStatusCheck() { + return healthService.getCassandraHealthNode().map(result-> + ApiResponse.builder() + .id("api.service.health.cassandra") + .params(ApiResponseParams.builder().build()) + .responseCode(HttpStatus.OK.name()) + .result(result) + .build()) + .map(ResponseEntity::ok); } @RequestMapping(value = "/health/kafka", method = RequestMethod.GET, produces = { "application/json", "text/json" }) - public ResponseEntity kafkaStatusCheck() throws IOException, JsonProcessingException { - ApiResponse response = ApiResponse.builder() - .id("api.service.health.kafka") - .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) - .result(healthService.getKafkaHealthNode()) - .build(); - - return ResponseEntity.ok(response); + public Mono> kafkaStatusCheck() { + return healthService.getKafkaHealthNode().map(result-> + ApiResponse.builder() + .id("api.service.health.kafka") + .params(ApiResponseParams.builder().build()) + .responseCode(HttpStatus.OK.name()) + .result(result) + .build()) + .map(ResponseEntity::ok); } @RequestMapping(value = "/health/campaign", method = RequestMethod.GET, produces = { "application/json", "text/json" }) - public ResponseEntity campaignUrlStatusCheck() throws JsonProcessingException, IOException { - ApiResponse response = ApiResponse.builder() - .id("api.service.health.campaign") - .params(ApiResponseParams.builder().build()) - .responseCode(HttpStatus.OK.name()) - .result(healthService.getCampaignUrlHealthNode()) - .build(); - - return ResponseEntity.ok(response); + public Mono> campaignUrlStatusCheck() { + return healthService.getCampaignUrlHealthNode().map(result -> + ApiResponse.builder().id("api.service.health.campaign") + .params(ApiResponseParams.builder().build()) + .responseCode(HttpStatus.OK.name()) + .result(result) + .build()) + .map(ResponseEntity::ok); } }