adam bien's blog

Simplest Possible Microprofile Liveness Check 📎

A health check can be exposed using the org.eclipse.microprofile.health.Health qualifier exposing a HealthCheckResponse:


import javax.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.health.Health;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;    
@Health
@ApplicationScoped
public class LivenessCheck implements HealthCheck {

    @Override
    public HealthCheckResponse call() {
        return HealthCheckResponse.
                named("ping").
                up().
                withData("duke", "lives").
                build();
    }

}

The health check is available from the "root" URL (not the WAR URI):

curl http://localhost:8080/health outputs:

{"outcome":"UP","checks":[{"name":"ping","state":"UP","data":{"duke":"lives"}}]}

The API is included in the microprofile BOM:


 <project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.airhacks</groupId>
<artifactId>microprofile</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<dependencies>
   <dependency>
       <groupId>org.eclipse.microprofile</groupId>
       <artifactId>microprofile</artifactId>
       <version>1.2</version>
       <type>pom</type>
       <scope>provided</scope>
   </dependency>
</dependencies>
<build>
   <finalName>microprofile</finalName>
</build>
<properties>
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
   <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
</project>    

The above example comes with 4.2 kB WAR and runs on stock Payara Server 5. Payara 5 comes with Java EE 8 and microprofile 1.2 support.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.