Table of contents
1.
Introduction
2.
About Dropwizard
3.
Health in Dropwizard
4.
Application Status
5.
HTTP & TCP Checks
6.
Health Data Access
7.
Frequently Asked Questions
7.1.
Does Dropwizard use Log4j?
7.2.
What are Dropwizard bundles?
7.3.
Is Dropwizard a framework?
7.4.
Who developed Dropwizard?
7.5.
Does Dropwizard use Tomcat?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Dropwizard-Health

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you ever created RESTful web services? Have to try that using any Java framework?

dropwizard

This article is focused on one of the frameworks of Java, which is known as Dropwizard. We will also study Dropwizard - Health. Let's see all these things in more detail.

About Dropwizard

logo

Dropwizard is an open-source Java framework. It is used to create high-performance RESTful web services quickly. It contains a few well-known libraries to make the lightweight package. It primarily uses the Jetty, Jackson, Jersey, JUnit, and Guava libraries. It also makes use of its own Metrics library.

Health in Dropwizard

There is a feature in Dropwizard called Health check. We can configure these health checks to create a view of the service health. Using health checks, we can drive decision-making using things like Kubernetes readiness & liveness check. You can also decide whether a load balancer should forward the traffic to your service or not.

health

This can be achieved by periodically conducting these dependence health checks. We can do it in the background in accordance with some schedule. Then we can combine the findings into a single signal of overall health. Other dependencies may be more non-essential to your service's ability to function. Let's say a cache, which may be considered more of a nice to have than a need. Some dependencies may be critical to your application running, such as a database. Your service can't function without these dependencies.

You can define the health check configurations as follows in your config.yaml file:

health:
  delayedShutdownHandlerEnabled: true
  shutdownWaitPeriod: 10s
  healthChecks:
    - name: user-database
      critical: true
    - name: user-notifications-queue
      critical: false
      schedule:
        checkInterval: 2500ms
        downtimeInterval: 10s
        failureAttempts: 2
        successAttempts: 1
    - name: user-cache
      critical: false

Application Status

status

Currently, there are just two status types that are provided:

status
  • Alive status: An alive status means the application is running normally. It can also be recovered from the stuck state without having to be restarted. Long-running applications occasionally reach a broken state. From this state they can only restart (e.g., deadlocked threads).
     
  • Ready status: The application is prepared to handle traffic when it has a ready status. Applications may momentarily be unable to handle traffic for a number of reasons. One reason is the necessity to build or compute huge caches during startup. The possibility of a vital dependency on an external service is another reason.
     

Now, assume you're using the configuration's default responder/responseProvider settings. Here is an example of how you would query the health check:

https://<hostname>:<port>/health-check?type=<type>&name=<name>
  • You can replace <type> with ready or alive. The default is ready if the type parameter is not given.
     
  • You can replace <name> with the name of the health check to query. You can also provide multiple names that can be provided, or no no names. If you want all checks, you can specify name=all to retrieve all checks.

HTTP & TCP Checks

The HttpHealthCheck or TcpHealthCheck classes make it simple to execute health checks. These checks are executed against dependencies. These dependencies offer either an HTTP or TCP health check interface if your service needs to do so.

Your health check(s) must be registered in the run() method of your Application class.

HTTP Check

@Override
public void run(final AppConfiguration configuration, final Environment environment) {
    ...
    environment.healthChecks().register("some-http-dependency", new HttpHealthCheck("http://some-http-dependency.com:8080/health-check"));
}
You can also try this code with Online Java Compiler
Run Code

TCP Check 

@Override
public void run(final AppConfiguration configuration, final Environment environment) {
    ...
    environment.healthChecks().register("some-tcp-dependency", new TcpHealthCheck("some-tcp-dependency.com", 443));
}
You can also try this code with Online Java Compiler
Run Code

Health Data Access

There are two ways to access views of the health state data in the Application.run() method:

By accessing data directly:

@Override
public void run(final AppConfiguration configuration, final Environment environment) {
    ...
    Collection<HealthStateView> views = environment.health().healthStateAggregator().healthStateViews();
}
You can also try this code with Online Java Compiler
Run Code

By listening to the data changes:

@Override
public void run(final AppConfiguration configuration, final Environment environment) {
    ...
    HealthStateListener myListener = new HealthStateListener() {
        @Override
        public void onStateChanged(String healthCheckName, boolean healthy) {
            System.out.println(healthCheckName + "changed state to " + healthy);
        }


        @Override
        public void onHealthyCheck(String healthCheckName) {
            System.out.println(healthCheckName + "is healthy! :)");
        }


        @Override
        public void onUnhealthyCheck(String healthCheckName) {
            System.out.println(healthCheckName + "is unhealthy! :(");
        }
    };
    environment.health().addHealthStateListener(myListener);
}
You can also try this code with Online Java Compiler
Run Code

Frequently Asked Questions

Does Dropwizard use Log4j?

Dropwizard makes use of Logback for its logging backend. It also provides an slf4j implementation. It even routes all Log4j, Java.util.logging, and Apache Commons Logging usage through Logback.

What are Dropwizard bundles?

A Dropwizard Bundle is a reusable collection of functionality. The Dropwizard project itself sometimes provides these functionalities. The purpose of these functionalities is to define blocks of an application's behavior.

Is Dropwizard a framework?

Yes, Dropwizard is an open-source Java framework. The purpose is to develop ops-friendly, high-performance RESTful backends.

Who developed Dropwizard?

Yammer developed Dropwizard to power their JVM-based backend.

Does Dropwizard use Tomcat?

Dropwizard recommends only running the Application with Jetty,. It does not officially support other web services like Tomcat.

Conclusion

In this article, we have studied one of the Java frameworks, which is known as Dropwizard, in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding Dropwizard and if you would like to learn more, check out our articles on Dropwizard JDBI3 and Dropwizard Client.  

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass