1. Overview

In this tutorial, we’re going to investigate how we can configure the connection pooling for Apache HttpClient 4.

By configuring the connection pool, we’ll make better use of the system resources and improve the client responsiveness.

2. Connection Pooling Support

Let’s first look at the HttpClient’s connection pooling support.

The HttpClient provides the PoolingHttpClientConnectionManager class to create a pool of connections.

Moreover, we can configure this pool by specifying different properties. Firstly, PoolingHttpClientConnectionManager provides a property to define the total number of available connections. This value is important when we have multiple clients using the same HttpClient instance. Secondly, PoolingHttpClientConnectionManager provides a property to define the maximum number of connections per route. For example, when we’re requesting a specific domain, this value will determine the performance of our application.

3. Default Connection Pool

Now, let’s continue with the default properties of PoolingHttpClientConnectionManager.

By default, the maximum number of connections is 20 and the maximum connection number per route is 2. However, these values are generally too low for real-world applications. For example, when all the connections are busy with handling other requests, HttpClient won’t create a new connection if the number exceeds 20. As a result, any class that tries to execute a request won’t get a connection. Instead, it’ll eventually get a ConnectionPoolTimeoutException exception.

public void executeWithDefaultHttpClient() throws Exception {
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        final HttpGet httpGet = new HttpGet(GET_URL);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}

Here, we’re creating the CloseableHttpClient instance using HttpClients.createDefault(). So we’ll get the default connection pool.

3. Configuring ConnectionPool

Next, we’ll look at how we can configure the connection pool.

3.1. Configuring the Connection Pool creating PoolingHttpClientConnectionManager

Firstly, we’ll configure the connection pool by directly creating an instance of PoolingHttpClientConnectionManager:

public void executeWithPooled() throws Exception {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(100);
    connectionManager.setDefaultMaxPerRoute(20);
    try (CloseableHttpClient httpClient = HttpClients.custom()
                                                     .setConnectionManager(connectionManager)
                                                     .build()) {
        final HttpGet httpGet = new HttpGet(GET_URL);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}

Here, we’re creating a connection pool with the maximum number of 100 connections and the maximum 20 connections per route. Then, we’re specifying this pool during the construction of HttpClient – HttpClients.custom().setConnectionManager().

3.2. Configuring the Connection Pool using HttpClientBuilder

Secondly, we’ll configure the connection pool using HttpClientBuilder.

The HttpClientBuilder class provides some shortcut configuration methods for setting total maximum connection and maximum connection per route:

public void executeWithPooledUsingHttpClientBuilder() throws Exception {
    try (CloseableHttpClient httpClient = HttpClients.custom()
                                                     .setMaxConnTotal(100)
                                                     .setMaxConnPerRoute(20)
                                                     .build()) {
        final HttpGet httpGet = new HttpGet(GET_URL);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}

Here, we’re using setMaxConnTotal() and setMaxConnPerRoute() methods to set the pool properties.

4. Summary

In this tutorial, we’ve looked at how we can configure the connection pool for Apache HttpClient 4.

Finally, check out the source code for all examples over on Github.

Leave a Reply

Close Menu