How to implement a custom Spring-Boot Application HealthIndicator

For those who use Spring-Boot or even better JHipster as based stack for their Java Enterprise Application, you are probably familiar with Spring-Boot « health » endpoint within Actuator project.

springboot-health-ok

If you want to read more about those endpoints, here is the only doc: http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

By default there are few health indicators (mail and database servers connectivity and free disk space) around which JHipster built a web interface.

Here i’m about to add a new health indicator to this monitoring page. In deed the application for which i integrated this change is an SMS and Call Scheduling web application that i built for a Research & Development Institute. This application interacts with two external APIs

  • a REST API provided by the Interactive Voice & Call Center platform i also develop (https://www.africasys.com) for the phone calls
  • a simple HTTP API provided by an SMS provider for (no surprise) the SMS

I needed to have a view on the availability of those APIs, absolutely necessary to global value delivery, to the health endpoint (and web page)

Now better than words, let’s see some codes🙂 (the whole classes are here on Github https://github.com/zoumhussein/Spring-Boot-CustomHealthIndicator)

/**
* @author zoumana
* @since 20160612
*
* AfricaSys custom HealthIndicator check for our FAI SMS provider APIs
*/
public class SMSProviderHealthIndicator extends AbstractHealthIndicator {

private final Logger log = LoggerFactory.getLogger(SMSProviderHealthIndicator.class);
private ContactService contactService;

public SMSProviderHealthIndicator(ContactService contactService) {
Assert.notNull(contactService, "contactService must not be null");
this.contactService = contactService;
}

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
log.debug("Initializing SMSProvider health indicator");
try {
contactService.getSmsService().getBalance();
builder.up();

} catch (Exception e) {
log.debug("Cannot connect to SMS API server. Error: {}", e.getMessage());
builder.down(e);
}
}
}

This is how it looks like on JHipster health web page (i shutdown the internet connectivity on my development environment of course :-)) to simulate outage. You can see that i can even view the exception

springboot-health

What is cool about this « health » REST endpoints, is that we can easily write probes and integrate them within our existing monitoring application (such as Nagio, Centreon, Chicken…). You just have to query the health check result as Sping-Boot is already taking care of regular checks runs.

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s