Apache HTTP Client Available Only for Two Connections

I have the code below to call the REST API method using the Apache HTTP client. However, only two concurrent requests can be sent using the above client. Is there any parameter for setting max connections?

HttpPost post = new HttpPost(resourcePath); addPayloadJsonString(payload, post);//set a String Entity setAuthHeader(post);// set Authorization: Basic header try { return httpClient.execute(post); } catch (IOException e) { String errorMsg = "Error while executing POST statement"; log.error(errorMsg, e); throw new RestClientException(errorMsg, e); } 

The boxes I use are below

 org.apache.httpcomponents.httpclient_4.3.5.jar org.apache.httpcomponents.httpcore_4.3.2.jar 
+1
rest apache
source share
1 answer

You can configure HttpClient with HttpClientConnectionManager

See Join Connection Manager .

ClientConnectionPoolManager supports the ClientConnectionPoolManager pool and can handle connection requests from multiple threads. Connections are united according to the route principle. Requesting a route that already has persistent manager connections for those available in the pool will be a service by leasing the connection from the pool, rather than creating a completely new connection.

PoolingHttpClientConnectionManager supports the maximum connection limit for each route and in general. By default, this implementation will create no more than two simultaneous connections for each route and no more than 20 connections. For many real-world applications, these restrictions may be too limited, especially if they use HTTP as the transport protocol for their services.

This example shows how you can configure connection pool settings:

 PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // Increase max total connection to 200 cm.setMaxTotal(200); // Increase default max connection per route to 20 cm.setDefaultMaxPerRoute(20); // Increase max connections for localhost:80 to 50 HttpHost localhost = new HttpHost("locahost", 80); cm.setMaxPerRoute(new HttpRoute(localhost), 50); CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(cm) .build(); 
+2
source

All Articles