CodexBloom - Programming Q&A Platform

Java 11: implementing Multiple Concurrent HTTP Requests Using Apache HttpClient and Connection Pooling

๐Ÿ‘€ Views: 89 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-08
java apache-httpclient http concurrency connection-pooling Java

Hey everyone, I'm running into an issue that's driving me crazy. I'm working with a question with making multiple concurrent HTTP requests using Apache HttpClient in Java 11. I have implemented a simple HTTP client that utilizes connection pooling, but I am getting a `java.net.ConnectException: Connection timed out` behavior intermittently when I exceed a certain number of simultaneous requests. Hereโ€™s a snippet of my code: ```java import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; public class HttpClientExample { private static final String URL = "http://example.com"; private static final int MAX_TOTAL_CONNECTIONS = 100; private static final int MAX_CONNECTIONS_PER_ROUTE = 20; public static void main(String[] args) throws InterruptedException { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS); connectionManager.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE); try (CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .build()) { Runnable task = () -> { try { HttpGet request = new HttpGet(URL); HttpResponse response = httpClient.execute(request); System.out.println("Response Code: " + response.getStatusLine().getStatusCode()); } catch (IOException e) { e.printStackTrace(); } }; // Create and start multiple threads for (int i = 0; i < 50; i++) { new Thread(task).start(); } } } } ``` When I run this code, it works fine for a small number of threads, but when I scale it up to 50 or more, I start seeing timeouts. The server Iโ€™m connecting to is capable of handling the load, as I've tested it using a load testing tool with a higher number of requests. I've tried increasing the connection pool settings and even added some delays between request initiations, but the scenario continues. Hereโ€™s the relevant output when the timeout occurs: ``` java.net.ConnectException: Connection timed out: connect ``` Iโ€™m also monitoring the server-side logs and not seeing any indications of connection issues. Is there a known limitation with Apache HttpClient regarding concurrent requests, or is there a different approach I should consider? Any help would be appreciated!