Table of contents
1.
Introduction
2.
Dropwizard Client
3.
Apache HttpClient
3.1.
Metrics
4.
Jersey Client
4.1.
Configuration
4.2.
Rx Usage
5.
Proxy Authentication
6.
Frequently Asked Questions
6.1.
What is the use of Dropwizard? 
6.2.
What distinguishes the spring boot from the Dropwizard?
6.3.
Is Dropwizard an open source?
6.4.
Does Dropwizard use Jetty?
6.5.
What is bootstrap in Dropwizard?
7.
Conclusion 
Last Updated: Mar 27, 2024
Medium

Dropwizard Client

Introduction

Are you also wondering what is Dropwizard and where are they used? Don’t worry Ninja. Let’s understand all about Dropwizard as well as Dropwizard Client.

Dropwizard is a lightweight RESTful Java framework based on the Jetty app server, Jersey REST framework, and Jackson JSON parser. Yammer developed the Dropwizard. It supports JVM(Java Virtual Machine) based backend. It provides the best Java libraries to embed in your applications.

Dropwizard Client

The dropwizard client module provides an easily configurable way to create clients in a Dropwizard project to call external REST (JSON) services.
In this article, we will be discussing the dropwizard Client and their working.

Dropwizard Client

The dropwizard-client module helps integrate your service with other web services using the Apache HttpClient and Jersey Client HTTP clients.

dropwizard client images
<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-client</artifactId>
</dependency>

Apache HttpClient

The underlying well-tested HTTP client library for dropwizard-client is Apache's HttpClient.

Your configuration class requires an HTTP client configuration instance to create a managed, instrumented HttpClient instance:

public class ExampleConfiguration extends Configuration {
    @Valid
    @NotNull
    private HttpClientConfiguration httpClient = new HttpClientConfiguration();
 
    @JsonProperty("httpClient")
    public HttpClientConfiguration getHttpClientConfiguration() {
        return httpClient;
    }
 
    @JsonProperty("httpClient")
    public void setHttpClientConfiguration(HttpClientConfiguration httpClient) {
        this.httpClient = httpClient;
    }
}
 
After creating the HttpClient instance, create a new HttpClientBuilder in your application's run method:

@Override
public void run(ExampleConfiguration config,
                Environment environment) {
    final HttpClient httpClient = new HttpClientBuilder(environment).using(config.getHttpClientConfiguration())
                                                                    .build(getName());
    environment.jersey().register(new ExternalServiceResource(httpClient));
}

Metrics

Dropwizard’s HttpClientBuilder gives you an instrumented subclass or its own library called Metrics which tracks the following pieces of data:

org.apache.http.conn.ClientConnectionManager.available-connections

The number of idle connections available for use in request execution.

org.apache.http.conn.ClientConnectionManager.leased-connections

This link tracks the number of persistent connections that are currently being used to execute the requests.

org.apache.http.conn.ClientConnectionManager.max-connections

The maximum number of connections allowed during the request.

org.apache.http.conn.ClientConnectionManager.pending-connections

Number of connection requests blocked while waiting for an open connection.

 

Apart from these, there are some more HTTP request which helps in tracking the rate at which these requests are being sent. Such as - GET, POST, HEAD, PUT, DELETE, TRACE, CONNECT, MOVE, PATCH, etc.

Must Read Apache Server

Jersey Client

Dropwizard also supports Jersey's Client API if HttpClient is too basic for you. You can utilize all server-side media type support in Jersey's Client, such as deserializing application/json request entities into POJOs (plain old Java objects).

Your configuration class requires a jersey client configuration instance to construct a managed, instrumented JerseyClient instance:

public class ExampleConfiguration extends Configuration {
    @Valid
    @NotNull
    private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();
 
    @JsonProperty("jerseyClient")
    public JerseyClientConfiguration getJerseyClientConfiguration() {
        return jerseyClient;
    }
 
    @JsonProperty("jerseyClient")
    public void setJerseyClientConfiguration(JerseyClientConfiguration jerseyClient) {
        this.jerseyClient = jerseyClient;
    }
}

After creating the JerseyClient instance, create a new JerseyClientBuilder in your service's run method:

@Override
public void run(ExampleConfiguration config,
                Environment environment) {
 
    final Client client = new JerseyClientBuilder(environment).using(config.getJerseyClientConfiguration())
                                                              .build(getName());
    environment.jersey().register(new ExternalServiceResource(client));
} 

Configuration

The Client created by Dropwizard deviates from the Jersey Client Configuration defaults. In Dropwizard, the default is 500 milliseconds. However, in Jersey, the default is for a client to never timeout reading or connecting during a request.

There are a few approaches to alter this behavior, one of which is to change the YAML configuration. Alternatively, you can change the properties on the JerseyClientConfiguration, which will impact all built clients. The configuration can be changed by using the property method on a per client basis, and here the Jersey Client Properties can be used.

Rx Usage

Jersey allows the creation of rx-clients to improve the ergonomics of asynchronous client requests. You can request Dropwizard to build a client like that (RxJava2):

@Override
public void run(ExampleConfiguration config,
                Environment environment) {

    final Client client =
        new JerseyClientBuilder(environment)
            .using(config.getJerseyClientConfiguration())
            .buildRx(getName(), RxFlowableInvokerProvider.class);
    //Any custom Service Resource that waits for Client in constructor
    environment.jersey().register(new ExternalServiceResource(client));
}

RxFlowableInvokerProvider.class is the JavaRx implementation and can be added to the pom:

<dependency>
    <groupId>org.glassfish.jersey.ext.rx</groupId>
    <artifactId>jersey-rx-client-rxjava2</artifactId>
</dependency>

Alternative implementations include JSR-166e, Guava, and RxJava.

The same thread pool that is used by conventional synchronous and asynchronous queries is used for rx requests by allowing Dropwizard the ability to create the rx-client.

Proxy Authentication

The client can use a forward proxy that supports basic and NTLM authentication. Basic Auth against a proxy is simple:

proxy:
     host: '192.168.52.11'
     port: 8080
     scheme : 'https'
     auth:
       username: 'secret'
       password: 'stuff'
     nonProxyHosts:
       - 'localhost'
       - '192.168.52.*'
       - '*.example.com'

 Configuring NTLM Auth involves changing the required Windows properties.

proxy:
     host: '192.168.52.11'
     port: 8080
     scheme : 'https'
     auth:
       username: 'secret'
       password: 'stuff'
       authScheme: 'NTLM'
       realm: 'realm' # optional, defaults to ANY_REALM
       hostname: 'workstation' # optional, defaults to null but may be required depending on your AD environment
       domain: 'HYPERCOMPUGLOBALMEGANET' # optional, defaults to null but may be required depending on your AD environment
       credentialType: 'NT'
     nonProxyHosts:
       - 'localhost'
       - '192.168.52.*'
       - '*.example.com'

FAQs

Frequently Asked Questions

What is the use of Dropwizard? 

Dropwizard is an open-source framework of Java generally used for the fast development of high-performance RESTful web services. It gathers popular libraries, like Jersey, Jackson, Jetty, JUnit, and Guava, to create the lightweight package. Furthermore, it uses its own library called Metrics.

What distinguishes the spring boot from the Dropwizard?

While Spring Boot uses the embeddable version of Tomcat by default but offers an alternative if you prefer Jetty or even RedHat's Undertow, on the other side, Dropwizard takes the convention over configuration approach a little further and is entirely built on Jetty.

Is Dropwizard an open source?

A high-performance, ops-friendly Java framework called Dropwizard can be used to create RESTful backends. Yammer developed it to power their JVM-based backend.

Does Dropwizard use Jetty?

Because HTTP is essential to build a web application, Dropwizard uses the Jetty HTTP framework to integrate an HTTP server into your project.

What is bootstrap in Dropwizard?

The bootstrap is a pre-start (temporary) application environment that contains all of the components needed to bootstrap a Dropwizard command.

Conclusion 

This article contains the basic information of the Dropwizard Client. In this article, we have discussed the two different web services: Apache HttpClient and Jersey Client. The reader can carry out a thorough understanding of the topic by referring to the Official Documentation. For more information on Chef and DevOps, Refer-

Check out the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series for more excellent content. Do upvote our blog to assist other ninjas in their development. 

Good luck with your coding!

Live masterclass