WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Boot & Hibernate · Advanced · question 60 of 100

Can you explain how to create and configure custom health check endpoints using Spring Boot Actuator?

📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

In a Spring Boot application, health check endpoints are used to check the status of the application and its components. Spring Boot Actuator provides several built-in health check endpoints that can be used out of the box. However, in some cases, it may be necessary to create custom health check endpoints for specific components or checks.

To create a custom health check endpoint in Spring Boot, we can follow these steps:

Create a new class that implements the HealthIndicator interface. This interface defines a single method, health(), which returns a Health object representing the current status of the component being checked.

Here is an example of a custom health check endpoint that checks the status of a remote service:

    
    @Component
    public class RemoteServiceHealthIndicator implements HealthIndicator {
        
        @Override
        public Health health() {
            boolean remoteServiceUp = checkRemoteServiceStatus();
            if (remoteServiceUp) {
                return Health.up().build();
            } else {
                return Health.down().withDetail("reason", "Remote service is not available").build();
            }
        }
        
        private boolean checkRemoteServiceStatus() {
            // perform some check to determine if remote service is available
            // return true if available, false otherwise
        }
    }

In this example, the health() method checks the status of a remote service by calling the checkRemoteServiceStatus() method. If the remote service is available, the health() method returns a Health object with an "UP" status. If the service is unavailable, it returns a Health object with a "DOWN" status and a custom message explaining the reason for the failure.

Register the new HealthIndicator bean with the Spring Boot application context.

We can do this by annotating the RemoteServiceHealthIndicator class with @Component or any other appropriate Spring bean annotation. Spring Boot will automatically detect and register the bean with the application context.

Access the custom health check endpoint via the /actuator/health endpoint.

Once the custom HealthIndicator bean is registered with the Spring Boot application context, it will be automatically included in the list of available health check endpoints provided by Spring Boot Actuator. To access the custom health check endpoint, we can make an HTTP GET request to the /actuator/health endpoint.

For example, if we assume that our application is running on localhost and the port number is 8080, we can make an HTTP GET request to http://localhost:8080/actuator/health to retrieve the health status of the application.

In conclusion, creating custom health check endpoints using Spring Boot Actuator is a straightforward process that can be accomplished by implementing the HealthIndicator interface and registering the new bean with the Spring Boot application context. This enables developers to easily monitor the health of their applications and specific components.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Boot & Hibernate interview — then scores it.
📞 Practice Spring Boot & Hibernate — free 15 min
📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

All 100 Spring Boot & Hibernate questions · All topics