How To Use Eclipse MicroProfile Health Checks

Photo by David Iskander on Unsplash

Today I decided to start to learn Eclipse MicroProfile. The first thing for every service is to at least have some sort of health check. 
So how do you add health checks to your JEE service using the MicroProfile API? First, you need to define the MicroProfile API as a dependency in maven:

Now we can create a new class called LivenessCheck and implement the HealthCheck interface:

It only requires you to implement the call() method, which will signal the status of our application. This status can only be one of two things: “UP” or “DOWN”.

HealthCheckResponse is used to build this status message. We can specify for which service a health check is done by calling named() using the service name.

The isHealthy() method could check on everything the service needs, for example, a database connection.

Now we are finished and can probe the health endpoint here: http://localhost:[PORT]/health.

The full example can be cloned from this repository and run using the buildAndRun.sh: https://github.com/florian42/microprofile-health.