mic*_*mia 6 java http httpclient apache-httpclient-4.x
我正在使用httpClient 4.3.6,其中包含CloseableHttpClient一个PoolingHttpClientConnectionManager.
我当前的设置包括200个线程,通过客户端同时执行GET请求.我正在尝试最大化线程每秒可以处理的请求数,但是一旦我开始执行超过~100/s,httpClient.execute()请求就会开始花费越来越多的时间来返回.我知道服务请求的机器没有减慢速度,问题的根源在于httpClient库或我机器上的资源.
这是我对客户端的实例化
// Start up an eviction thread.
// remove idle (for 50ms) connections every 50 ms
IdleConnectionMonitorThread monitor = new IdleConnectionMonitorThread(cm);
// Don't stop quitting.
monitor.setDaemon(true);
monitor.start();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// increase connection limit
cm.setMaxTotal(2000);
cm.setDefaultMaxPerRoute(2000);
// create client
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(RequestConfig.custom().build());
builder.setConnectionManager(cm);
this.httpClient = builder.build();
Run Code Online (Sandbox Code Playgroud)
execute当我开始执行线程时,该方法的平均执行时间稳定增加但缓慢增加,并在请求率下降后立即快速下降.
HttpGet getRequest = new HttpGet(uri);
HttpClientContext context = HttpClientContext.create();
try(CloseableHttpResponse response = httpClient.execute(getRequest, context);) {
int returnStatus = response.getStatusLine().getStatusCode();
switch (returnStatus){
case 404:
// deal with 404
case 200:
HttpEntity entity = response.getEntity();
if (entity != null) {
entity = new BufferedHttpEntity(entity);
InputStream instream = entity.getContent();
try{
// deal with instream
} finally {
instream.close();
// make sure everything is consumed
EntityUtils.consume(entity);
}
} else {
// throw exception
}
default:
// weird codes
}
}
Run Code Online (Sandbox Code Playgroud)