Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.Jinus.scheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class HealthCheckScheduler {
private static final Logger logger = LoggerFactory.getLogger(HealthCheckScheduler.class);

@Value("${server.port:8080}")
private int serverPort;

private final RestTemplate restTemplate = new RestTemplate();

@Scheduled(fixedRate = 60000) // 60초마다 실행
public void performHealthCheck() {
String url = "http://localhost:" + serverPort + "/actuator/health";
try {
restTemplate.getForEntity(url, String.class);
} catch (Exception e) {
Comment on lines +20 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add response handling to verify health status.

The current implementation only confirms that the endpoint is reachable but doesn't verify the actual health status. Spring Boot's actuator returns JSON with a status field that should be checked.

    public void performHealthCheck() {
        String url = "http://localhost:" + serverPort + "/actuator/health";
        try {
-           restTemplate.getForEntity(url, String.class);
+           ResponseEntity<Map> response = restTemplate.getForEntity(url, Map.class);
+           String status = (String) ((Map) response.getBody()).get("status");
+           if (!"UP".equals(status)) {
+               logger.warn("Application health check returned status: {}", status);
+           } else {
+               logger.debug("Health check successful: status UP");
+           }
        } catch (Exception e) {
            logger.error("Health check failed: {}", e.getMessage(), e);
        }
    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
String url = "http://localhost:" + serverPort + "/actuator/health";
try {
restTemplate.getForEntity(url, String.class);
} catch (Exception e) {
public void performHealthCheck() {
String url = "http://localhost:" + serverPort + "/actuator/health";
try {
ResponseEntity<Map> response = restTemplate.getForEntity(url, Map.class);
String status = (String) ((Map) response.getBody()).get("status");
if (!"UP".equals(status)) {
logger.warn("Application health check returned status: {}", status);
} else {
logger.debug("Health check successful: status UP");
}
} catch (Exception e) {
logger.error("Health check failed: {}", e.getMessage(), e);
}
}

logger.error("Health check failed: {}", e.getMessage(), e);
}
}
}
Loading