我有以下代码来使用Apache HTTP客户端调用REST API方法。但是,只能使用上述客户端发送两个并行请求。 是否有任何参数来设置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);
}
我正在使用的罐子是,
org.apache.httpcomponents.httpclient_4.3.5.jar
org.apache.httpcomponents.httpcore_4.3.2.jar
答案 0 :(得分:4)
您可以使用HttpClientConnectionManager
ClientConnectionPoolManager
维护一个HttpClientConnections
池,并且能够为来自多个执行线程的连接请求提供服务。连接以每个路由为基础进行池化。对于已经管理器具有池中可用连续连接的路由的请求将是通过从池租用连接而不是创建全新连接的服务。
PoolingHttpClientConnectionManager
维持每个路由和总计的最大连接数限制。默认情况下,此实现将为每个给定路由创建不超过2个并发连接,并且总共不再有20个连接。对于许多实际应用程序,这些限制可能会受到太多限制,特别是如果他们使用HTTP作为其服务的传输协议。
此示例显示如何调整连接池参数:
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();