Adding health checks

Exclusive offer: get 50% off this eBook here
RESTful Web Services with Dropwizard

RESTful Web Services with Dropwizard — Save 50%

Over 20 recipes to help you build high-performance, production-ready RESTful JVM-based backend services with this book and ebook

$20.99    $10.50
by Alexandros Dallas | February 2014 | Open Source Web Development

This article by Alexandros Dallas, the author of RESTful Web Services with Dropwizard, deals with the implementation of runtime tests for our application, which are known as health checks. You will be guided through the implementation of a health check that ensures that your HTTP client can indeed interact with a web service.

(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:


RESTful Web Services with Dropwizard Over 20 recipes to help you build high-performance, production-ready RESTful JVM-based backend services with this book and ebook
Published: February 2014
eBook Price: $20.99
Book Price: $34.99
See more
Select your format and quantity:

About the Author :


Alexandros Dallas

Alexandros Dallas studied Applied Informatics in Management and Economy and is now a software test engineer based in Athens.

He has a solid programming/software development background, and whenever he is free, he spends his time contributing to open source projects.

He is well aware of Dropwizard's core libraries, such as Jersey, since his interests include the development and integration of web APIs.

Books From Packt


 Developing RESTful Web Services with Jersey 2.0
Developing RESTful Web Services with Jersey 2.0

RESTful PHP Web Services
RESTful PHP Web Services

 RESTful Java Web Services
RESTful Java Web Services

 Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON
Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON

 ASP.NET Web API: Build RESTful web applications and services on the .NET framework
ASP.NET Web API: Build RESTful web applications and services on the .NET framework

Web Services Testing with soapUI
Web Services Testing with soapUI

Spring Web Services 2 Cookbook
Spring Web Services 2 Cookbook

Apache Axis2 Web Services, 2nd Edition
Apache Axis2 Web Services, 2nd Edition


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software