Adding health checks

The world is moving towards RESTful Web Services and now you can get up to speed quickly and painlessly. This tutorial covers all you need to create your own Web Service applications using the Dropwizard libraries.

(For more resources related to this topic, see here.)

A health check is a runtime test for our application. We are going to create a health check that tests the creation of new contacts using the Jersey client.

The health check results are accessible through the admin port of our application, which by default is 8081.

How to do it…

To add a health check perform the following steps:

  1. Create a new package called com.dwbook.phonebook.health and a class named NewContactHealthCheck in it:

    import javax.ws.rs.core.MediaType; import com.codahale.metrics.health.HealthCheck; import com.dwbook.phonebook.representations.Contact; import com.sun.jersey.api.client.*; public class NewContactHealthCheck extends HealthCheck { private final Client client; public NewContactHealthCheck(Client client) { super(); this.client = client; } @Override protected Result check() throws Exception { WebResource contactResource = client .resource("http://localhost:8080/contact"); ClientResponse response = contactResource.type( MediaType.APPLICATION_JSON).post( ClientResponse.class, new Contact(0, "Health Check First Name", "Health Check Last Name", "00000000")); if (response.getStatus() == 201) { return Result.healthy(); } else { return Result.unhealthy("New Contact cannot be created!"); } } }

  2. Register the health check with the Dropwizard environment by using the HealthCheckRegistry#register() method within the #run() method of the App class. You will first need to import com.dwbook.phonebook.health.NewContactHealthCheck. The HealthCheckRegistry can be accessed using the Environment#healthChecks() method:

    // Add health checks e.healthChecks().register ("New Contact health check", new NewContactHealthCheck(client));

  3. After building and starting your application, navigate with your browser to http://localhost:8081/healthcheck:

The results of the defined health checks are presented in the JSON format. In case the custom health check we just created or any other health check fails, it will be flagged as "healthy": false, letting you know that your application faces runtime problems.

How it works…

We used exactly the same code used by our client class in order to create a health check; that is, a runtime test that confirms that the new contacts can be created by performing HTTP POST requests to the appropriate endpoint of the ContactResource class. This health check gives us the required confidence that our web service is functional.

All we need for the creation of a health check is a class that extends HealthCheck and implements the #check() method. In the class's constructor, we call the parent class's constructor specifying the name of our check—the one that will be used to identify our health check.

In the #check() method, we literally implement a check. We check that everything is as it should be. If so, we return Result.healthy(), else we return Result.unhealthy(), indicating that something is going wrong.

Summary

This article showed what a health check is and demonstrated how to add a health check. The health check we created tested the creation of new contacts using the Jersey client.

Resources for Article:


Further resources on this subject:


Books to Consider

comments powered by Disqus