I cannot find any documentation on when Springs Health Actuator will return the UP status. Can you rely on all @Components being initialized? Will a @Controller be ready to serve requests?
In order to test whether the application context is loaded, you can perform this custom implementation.
@EventListener - The method annotated with EventListener is triggered when the context is initialized.
In the below code, I have incremented the counter on successful initialization. When you hit the actuator health endpoint, you will get the status as UP.
Sample implementation for context initialization health check:-
@Component
public class ApplicationContextHealthIndicator implements HealthIndicator {
private final Logger LOG = Logger.getLogger(getClass().getName());
public static int counter;
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
LOG.info("Increment counter :" + counter);
counter++;
}
@Override
public Health health() {
if (counter == 0) {
return Health.down().withDetail("Error Code", 500).build();
}
return Health.up().build();
}
}
Endpoint Spring boot version 5:-
http://localhost:8080/actuator/health
Spring actuator can check the database, web service endpoints, email server etc. It can provide the status for all these resources which are part of the application.
Some default health indicators available
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With