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();
}
}
}
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).
3 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @Steadhaven! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Peter Rader
Peter Rader5mo ago
There is a practice called a Heartbeat for the application periodically telling someone that he is still "alive". For long running tasks you could use in-code a Watchdog (org.apache.commons.exec.Watchdog). Also a CountDownLatch (java.util.concurrent.CountDownLatch) could help notify stages of long running blocks.
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?