Apache HTTP client only two connections are possib

2019-05-29 04:20发布

I have below code to invoke a REST API method using Apache HTTP client. However only two parallel requests could be sent using above client. Is there any parameter to set 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);
        }

Jars I am using are below are,

org.apache.httpcomponents.httpclient_4.3.5.jar
org.apache.httpcomponents.httpcore_4.3.2.jar

1条回答
别忘想泡老子
2楼-- · 2019-05-29 04:45

You can configure the HttpClient with HttpClientConnectionManager

Take a look at Pooling connection manager.

ClientConnectionPoolManager maintains a pool of HttpClientConnections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis. A request for a route which already the manager has persistent connections for available in the pool will be services by leasing a connection from the pool rather than creating a brand new connection.

PoolingHttpClientConnectionManager maintains a maximum limit of connections on a per route basis and in total. Per default this implementation will create no more than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services.

This example shows how the connection pool parameters can be adjusted:

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();
查看更多
登录 后发表回答