1. Overview
In this tutorial, we'll go over the basics of connection management within HttpClient 4.
We'll cover the use of BasicHttpClientConnectionManager and PoolingHttpClientConnectionManager to enforce a safe, protocol-compliant and efficient use of HTTP connections.
2. The BasicHttpClientConnectionManager for a Low-Level, Single-Threaded Connection
The BasicHttpClientConnectionManager is available since HttpClient 4.3.3 as the simplest implementation of an HTTP connection manager.
We use it to create and manage a single connection that only one thread can use at a time.
HttpClient configurations for advanced use cases.
How to send Custom Cookies with the Apache HttpClient.
Example of how to configure the HttpClient with SSL.
Example 2.1. Getting a Connection Request for a Low-Level Connection (HttpClientConnection)
BasicHttpClientConnectionManager connManager
= new BasicHttpClientConnectionManager();
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
ConnectionRequest connRequest = connManager.requestConnection(route, null);
The requestConnection method gets from the manager a pool of connections for a specific route to connect to. The route parameter specifies a route of “proxy hops” to the target host, or the target host itself.
It is possible to run a request using an HttpClientConnection directly. However, keep in mind this low-level approach is verbose and difficult to manage. Low-level connections are useful to access socket and connection data such as timeouts and target host information. But for standard executions, the HttpClient is a much easier API to work against.
3. Using the PoolingHttpClientConnectionManager to Get and Manage a Pool of Multithreaded Connections
The PoolingHttpClientConnectionManager will create and manage a pool of connections for each route or target host we use. The default size of the pool of concurrent connections that can be open by the manager is two for each route or target host and 20 for total open connections.
First, let's take a look at how to set up this connection manager on a simple HttpClient:
Example 3.1. Setting the PoolingHttpClientConnectionManager on an HttpClient
HttpClientConnectionManager poolingConnManager
= new PoolingHttpClientConnectionManager();
CloseableHttpClient client
= HttpClients.custom().setConnectionManager(poolingConnManager)
.build();
client.execute(new HttpGet("/"));
assertTrue(poolingConnManager.getTotalStats().getLeased() == 1);
Next, let's see how the same connection manager can be used by two HttpClients running in two different threads:
Example 3.2. Using Two HttpClients to Connect to One Target Host Each
HttpGet get1 = new HttpGet("/");
HttpGet get2 = new HttpGet("http://google.com");
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
CloseableHttpClient client1
= HttpClients.custom().setConnectionManager(connManager).build();
CloseableHttpClient client2
= HttpClients.custom().setConnectionManager(connManager).build();
MultiHttpClientConnThread thread1
= new MultiHttpClientConnThread(client1, get1);
MultiHttpClientConnThread thread2
= new MultiHttpClientConnThread(client2, get2);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
Notice that we're using a very simple custom thread implementation:
Example 3.3. Custom Thread Executing a GET Request
public class MultiHttpClientConnThread extends Thread {
private CloseableHttpClient client;
private HttpGet get;
// standard constructors
public void run(){
try {
HttpResponse response = client.execute(get);
EntityUtils.consume(response.getEntity());
} catch (ClientProtocolException ex) {
} catch (IOException ex) {
}
}
}
Notice the EntityUtils.consume(response.getEntity) call. This is necessary to consume the entire content of the response (entity) so that the manager can release the connection back to the pool.
4. Configure the Connection Manager
The defaults of the pooling connection manager are well chosen. But, depending on our use case, they may be too small.
So, let's see how we can configure
- the total number of connections
- the maximum number of connections per (any) route
- the maximum number of connections per a single, specific route
Example 4.1. Increasing the Number of Connections That Can Be Open and Managed Beyond the default Limits
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(5);
connManager.setDefaultMaxPerRoute(4);
HttpHost host = new HttpHost("www.baeldung.com", 80);
connManager.setMaxPerRoute(new HttpRoute(host), 5);
Let's recap the API:
- setMaxTotal(int max) – Set the maximum number of total open connections
- setDefaultMaxPerRoute(int max) – Set the maximum number of concurrent connections per route, which is two by default
- setMaxPerRoute(int max) – Set the total number of concurrent connections to a specific route, which is two by default
So, without changing the default, we're going to reach the limits of the connection manager quite easily.
Let's see what that looks like:
Example 4.2. Using Threads to Execute Connections
HttpGet get = new HttpGet("http://www.baeldung.com");
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom().
setConnectionManager(connManager).build();
MultiHttpClientConnThread thread1
= new MultiHttpClientConnThread(client, get);
MultiHttpClientConnThread thread2
= new MultiHttpClientConnThread(client, get);
MultiHttpClientConnThread thread3
= new MultiHttpClientConnThread(client, get);
thread1.start();
thread2.start();
thread3.start();
thread1.join();
thread2.join();
thread3.join();
Remember that the per host connection limit is two by default. So, in this example, we want three threads to make three requests to the same host, but only two connections will be allocated in parallel.
Let's take a look at the logs.
We have three threads running but only two leased connections:
[Thread-0] INFO o.b.h.c.MultiHttpClientConnThread
- Before - Leased Connections = 0
[Thread-1] INFO o.b.h.c.MultiHttpClientConnThread
- Before - Leased Connections = 0
[Thread-2] INFO o.b.h.c.MultiHttpClientConnThread
- Before - Leased Connections = 0
[Thread-2] INFO o.b.h.c.MultiHttpClientConnThread
- After - Leased Connections = 2
[Thread-0] INFO o.b.h.c.MultiHttpClientConnThread
- After - Leased Connections = 2
5. Connection Keep-Alive Strategy
According to the HttpClient 4.3.3. reference: “If the Keep-Alive
header is not present in the response, HttpClient assumes the connection can be kept alive indefinitely.” (See the HttpClient Reference).
To get around this and be able to manage dead connections, we need a customized strategy implementation and to build it into the HttpClient.
Example 5.1. A Custom Keep-Alive Strategy
ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator
(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase
("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return 5 * 1000;
}
};
This strategy will first try to apply the host's Keep-Alive policy stated in the header. If that information is not present in the response header, it will keep alive connections for five seconds.
Now let's create a client with this custom strategy:
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setKeepAliveStrategy(myStrategy)
.setConnectionManager(connManager)
.build();
6. Connection Persistence/Reuse
The HTTP/1.1 Spec states that we can reuse connections if they have not been closed. This is known as connection persistence.
Once the manager releases a connection, it stays open for reuse.
When using a BasicHttpClientConnectionManager, which can only mange a single connection, the connection must be released before it is leased back again:
Example 6.1. BasicHttpClientConnectionManager Connection Reuse
BasicHttpClientConnectionManager basicConnManager =
new BasicHttpClientConnectionManager();
HttpClientContext context = HttpClientContext.create();
// low level
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
basicConnManager.connect(conn, route, 1000, context);
basicConnManager.routeComplete(conn, route, context);
HttpRequestExecutor exeRequest = new HttpRequestExecutor();
context.setTargetHost((new HttpHost("www.baeldung.com", 80)));
HttpGet get = new HttpGet("http://www.baeldung.com");
exeRequest.execute(get, conn, context);
basicConnManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS);
// high level
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(basicConnManager)
.build();
client.execute(get);
Let's take a look at what happens.
Notice that we use a low-level connection first, just so that we have full control over when we release the connection, and then a normal higher-level connection with an HttpClient.
The complex low-level logic is not very relevant here. The only thing we care about is the releaseConnection call. That releases the only available connection and allows it to be reused.
Then the client runs the GET request again with success.
If we skip releasing the connection, we will get an IllegalStateException from the HttpClient:
java.lang.IllegalStateException: Connection is still allocated
at o.a.h.u.Asserts.check(Asserts.java:34)
at o.a.h.i.c.BasicHttpClientConnectionManager.getConnection
(BasicHttpClientConnectionManager.java:248)
Note that the existing connection isn't closed, just released and then reused by the second request.
In contrast to the above example, the PoolingHttpClientConnectionManager allows connection reuse transparently without the need to release a connection implicitly:
Example 6.2. PoolingHttpClientConnectionManager: Reusing Connections With Threads
HttpGet get = new HttpGet("http://echo.200please.com");
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(5);
connManager.setMaxTotal(5);
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager)
.build();
MultiHttpClientConnThread[] threads
= new MultiHttpClientConnThread[10];
for(int i = 0; i < threads.length; i++){
threads[i] = new MultiHttpClientConnThread(client, get, connManager);
}
for (MultiHttpClientConnThread thread: threads) {
thread.start();
}
for (MultiHttpClientConnThread thread: threads) {
thread.join(1000);
}
The example above has 10 threads running 10 requests but only sharing 5 connections.
Of course, this example relies on the server's Keep-Alive timeout. To make sure the connections don't die before reuse, we should configure the client with a Keep-Alive strategy (See Example 5.1.).
7. Configuring Timeouts – Socket Timeout Using the Connection Manager
The only timeout that we can set when we configure the connection manager is the socket timeout:
Example 7.1. Setting Socket Timeout to 5 Seconds
HttpRoute route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
connManager.setSocketConfig(route.getTargetHost(),SocketConfig.custom().
setSoTimeout(5000).build());
For a more in-depth discussion of timeouts in the HttpClient, see here.
8. Connection Eviction
We use connection eviction to detect idle and expired connections and close them. We have two options to do this:
- Rely on the HttpClient to check if the connection is stale before running a request. This is an expensive option that is not always reliable.
- Create a monitor thread to close idle and/or closed connections
Example 8.1. Setting the HttpClient to Check for Stale Connections
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(
RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()
).setConnectionManager(connManager).build();
Example 8.2. Using a Stale Connection Monitor Thread
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager).build();
IdleConnectionMonitorThread staleMonitor
= new IdleConnectionMonitorThread(connManager);
staleMonitor.start();
staleMonitor.join(1000);
Let's look at the IdleConnectionMonitorThread class:
public class IdleConnectionMonitorThread extends Thread {
private final HttpClientConnectionManager connMgr;
private volatile boolean shutdown;
public IdleConnectionMonitorThread(
PoolingHttpClientConnectionManager connMgr) {
super();
this.connMgr = connMgr;
}
@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(1000);
connMgr.closeExpiredConnections();
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
} catch (InterruptedException ex) {
shutdown();
}
}
public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll();
}
}
}
9. Connection Closing
We can close a connection gracefully (we make an attempt to flush the output buffer prior to closing), or we can do it forcefully, by calling the shutdown method (the output buffer is not flushed).
To properly close connections, we need to do all of the following:
- Consume and close the response (if closeable)
- Close and shut down the connection manager
Example 9.1. Closing Connection and Releasing Resources
connManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager).build();
HttpGet get = new HttpGet("http://google.com");
CloseableHttpResponse response = client.execute(get);
EntityUtils.consume(response.getEntity());
response.close();
client.close();
connManager.close();
If we shut down the manager without closing connections already, all connections will be closed and all resources released.
It's important to keep in mind that this will not flush any data that may have been ongoing for the existing connections.
10. Conclusion
In this article, we discussed how to use the HTTP Connection Management API of HttpClient to handle the entire process of managing connections. This included opening and allocating them, managing their concurrent use by multiple agents and finally closing them.
We saw how the BasicHttpClientConnectionManager is a simple solution to handle single connections and how it can manage low-level connections.
We also saw how the PoolingHttpClientConnectionManager combined with the HttpClient API provide for an efficient and protocol-compliant use of HTTP connections.
The code used in this article can be found over on GitHub.
Course – LS (cat=HTTP Client-Side) res – HTTP Client (eBook) (cat=Http Client-Side)