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.

Create and use HTTP Client connection pool
Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
9Share on Google+
Google+
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

4 thoughts on “Create and use HTTP Client connection pool

  1. 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.

  2. 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.