Create and use HTTP Client connection pool

What is the HTTP client? For example, if you want to access a website, you are a client. You type the URL of the website into the browser, and you get the content of that website in the browser. Most websites use the HTTP protocol, and when you visit those sites, we can call you an HTTP client.

So what is the connection pool? For example, you are writing a program that accesses a website several times over a certain period of time. You repeatedly open the connection to that website, receive the content and then disconnect. It would be better if we had a place containing the connection to that website, and then we just took the content of the website without considering the connection to that website. And the place holds our connection to the website, we call the pool. We just need to get the connection from that pool to use only.

In this tutorial, I will show you how to create an HTTP client connection pool using  HTTP Client library from the Apache organization and how to use that connection pool.

OK, let’s get started!

Create and use HTTP Client connection pool

Now we will add the dependency of the HTTP client library.

To see the difference between using a connection pool and not using a connection pool, I first write the code to create a normal HTTP client, without using the connection pool to see the results. Of course, I still use the HTTP client library.

The content is as follow:

I’m using a CloseableHttpClient object that acts as an HTTP client, which means that the object sends the request to a website. In this example, I run a website on my computer and will send 10 requests to this website at the same time.

To verify how many requests to the website in our example, we will run the following statement using the command line:

Result:

Create and use HTTP Client connection pool

This is the number of connections to port 8080 on my computer before I run the above code. Currently, there are 2 connections, now I will run the example code to see how the connection number will increase.

And this is the result:

Create and use HTTP Client connection pool

As you can see, there are 10 more connections to port 8080 on my computer.

Now, I’m going to write code to switch to connection pool:

The HttpClient library provides us with an object called PoolingHttpClientConnectionManager that allows us to manage the number of connections to a given website. We can configure the maximum number of connections to a particular website, or the total number of connections our application can open to connect to different websites.

To put the PoolingHttpClientConnectionManager object into the HttpClient object we use the setConnectionManager() method.

And below is the result when we use the connection pool:

Create and use HTTP Client connection pool

You see, there is only one connection open between our application and the website which we connect.

5/5 - (3 votes)

9 thoughts on “Create and use HTTP Client connection pool

  1. Hi,

    Thanks for the article. Is it possible to set/reset the headers along with using the same connection from connection pool. Does it create 10 different connections or does it use same connection for same URL?

  2. I dont’t think you demo is really perfect. httpclient in version 4.5.2.HttpClients.createSystem() will use default connection pool.
    if (this.systemProperties) {
    String s = System.getProperty(“http.keepAlive”, “true”);
    if (“true”.equalsIgnoreCase(s)) {
    s = System.getProperty(“http.maxConnections”, “5”);
    int max = Integer.parseInt(s);
    poolingmgr.setDefaultMaxPerRoute(max);
    poolingmgr.setMaxTotal(2 * max);
    }
    }

  3. Tự hào VN quá b ơi. Link này của bạn được chuyên gia microsoft gửi cho bọn mình tham khảo này. Keep doing the good job nhé.

  4. Hello thanks for that artical, can you also explain whats difference between connection timeout of client anf connection timeout property of connection manager.
    If a request comes and all connection objects are used up, so which timeout starts ticking? Or it starts when it gets connection object and wait to establish connection.

  5. Thanks for nice post… How to re-user the “HttpClientExample” class that is being called from other classes and these class is calling some X REST End Point to read and parse content.

Add Comment