背景:日志报错:
参考:
(一)
HttpClient4.X Invalid use of SingleClientConnManager: connection still allocated
项目用到了httpclient4.1开发。在多线程的情况下,异常报
Invalid use of SingleClientConnManager: connection still allocated
从国外的一张帖子看到,httpclient默认使用的是SingleClientConnManager,但在并发环境下最好使用ThreadSafeClientConnManager。代码如下:
HttpClient client = new DefaultHttpClient(new ThreadSafeClientConnManager());
HttpGet httpGet = null;
(二)
- ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
- cm.setMaxTotal(100);
- HttpClient httpclient = new DefaultHttpClient(cm);
于是改代码为
static
{
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
cm.setMaxTotal(100);
httpclient = new DefaultHttpClient(cm);
httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient);
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
过两天看看效果吧,最晚一个error是 2.28 10:21
3.2 未再出现过同类error