Custom Spring Boot actuator HealthIndicator

I am reading a book that explains how to create a custom HealthIndicator in Spring Boot, but I don't find it satisfactory. It is calling an external API, and immediately just returns the health status as either up or down based on that.
package com.manning.sbip.ch04.health.indicator;
 
// imports
 
@Component
public class DogsApiHealthIndicator implements HealthIndicator {
   
    @Override
    public Health health() {
        try {
            ParameterizedTypeReference<Map<String, String>> reference
➥ = new ParameterizedTypeReference<Map<String, String>>() {};
            ResponseEntity<Map<String, String>> result
➥ = new RestTemplate().exchange
➥ ("https:/ /dog.ceo/api/breeds/image/random",
➥ HttpMethod.GET, null, reference);
            if (result.getStatusCode().is2xxSuccessful() &&
➥ result.getBody() != null) {
                return Health.up().withDetails(result.getBody()).build();
            }
            else {
                return Health.down().withDetail("status",
➥ result.getStatusCode()).build();
            }
        }
        catch(RestClientException ex) {
             return Health.down().withException(ex).build();
        }
    }
}


Often, you need to check for the health of very specific components and services in your application. Or even specific code snippets running somewhere, that other applications may depend on. Furthermore, this can sometimes take a certain time to run, so the health status ought to be "down" while the specific internal code is still running and then return "up" when its up.

TL;DR: How can I create a custom HealthIndicator for specific code parts in my Spring Boot application? It has to be able to check more than once, e.g. if docker calls on the health end point it ought to check if its done yet or not. (my own scenario is that my app is doing some liquibase migration at its startup, and other docker services should wait for it to finish before they start theirs).
Was this page helpful?