错误:org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool

前言 :
第一次看到这个错误, 上网找了下,有文章说是连接池不够了。。。。 并没有多想,立即将原有程序的 链接池扩容了3倍,然后单个路由 扩容了5倍。
问题解决, 以为找到了,答案。 但是 过了大约 几天之后,再次出现该问题,当时就特别疑惑, 没有扩容之前 程序已经运行了 将近两年,并没有发生任何错误,
现在 扩容了,竟然还报这个错, 此时 分析方向 应该发生改变。

怀疑方向一:大量的Timeout 出现,是否 请求的域名有问题 ?
于是,手动 ping 了访问域名,发现并没有多慢. (疑问否决)
怀疑方向二:是否我们服务器出口IP 有问题, 请求 指定域名 超时?
于是: 我们新开了一台机器, 然后将 原有的两台机器 停掉一台, 并且新开的机器 使用 全新的出口IP, 以区分于原有机器,后来发现 新卡的机器,没有问题。
因此,我们大致定位了问题,就是 服务器的出口IP 有问题, 并不是程序的问题。 所以,解决方案就是 找运维 重新申请配置了机器.

下面 贴出 我连接池的 写法, 做个小笔记 。
HttpClient 版本如下

       <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>

连接池代码如下:

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; public class HttpClientFactory { private static final Integer MAX_TOTAL = 300; //连接池最大连接数
private static final Integer MAX_PER_ROUTE = 50; //单个路由默认最大连接数
private static final Integer REQ_TIMEOUT = 5 * 1000; //请求超时时间ms
private static final Integer CONN_TIMEOUT = 5 * 1000; //连接超时时间ms
private static final Integer SOCK_TIMEOUT = 10 * 1000; //读取超时时间ms
private static HttpClientConnectionMonitorThread thread; //HTTP链接管理器线程 public static HttpClientConnectionMonitorThread getThread() {
return thread;
}
public static void setThread(HttpClientConnectionMonitorThread thread) {
HttpClientFactory.thread = thread;
} public static HttpClient createSimpleHttpClient(){
SSLConnectionSocketFactory sf = SSLConnectionSocketFactory.getSocketFactory();
return HttpClientBuilder.create()
.setSSLSocketFactory(sf)
.build();
} public static HttpClient createHttpClient() {
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(MAX_TOTAL);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(MAX_PER_ROUTE);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(REQ_TIMEOUT)
.setConnectTimeout(CONN_TIMEOUT).setSocketTimeout(SOCK_TIMEOUT)
.build();
HttpClientFactory.thread=new HttpClientConnectionMonitorThread(poolingHttpClientConnectionManager); //管理 http连接池
return HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).setDefaultRequestConfig(requestConfig).build();
}
}

以下 线程用来清理 连接池无效的链接 :

import java.util.concurrent.TimeUnit;
import org.apache.http.conn.HttpClientConnectionManager; /**
* <p>Description: 使用管理器,管理HTTP连接池 无效链接定期清理功能</p>
*/
public class HttpClientConnectionMonitorThread extends Thread { private final HttpClientConnectionManager connManager;
private volatile boolean shutdown; public HttpClientConnectionMonitorThread(HttpClientConnectionManager connManager) {
super();
this.setName("http-connection-monitor");
this.setDaemon(true);
this.connManager = connManager;
this.start();
} @Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(5000); // 等待5秒
// 关闭过期的链接
connManager.closeExpiredConnections();
// 选择关闭 空闲30秒的链接
connManager.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
} catch (InterruptedException ex) {
}
} /**
* 方法描述: 停止 管理器 清理无效链接 (该方法当前暂时关闭)
* @author andy 2017年8月28日 下午1:45:18
*/
@Deprecated
public void shutDownMonitor() {
synchronized (this) {
shutdown = true;
notifyAll();
}
} }

HttpClient大并发下Timeout waiting for connection from pool 问题解决方案  http://blog.csdn.net/duxing_langzi/article/details/77772673

今天解决了一个HttpClient的异常,汗啊,一个HttpClient使用稍有不慎都会是毁灭级别的啊。
这里有之前因为route配置不当导致服务器异常的一个处理:http://blog.csdn.net/shootyou/article/details/6415248
里面的HttpConnectionManager实现就是我在这里使用的实现。

问题表现:
tomcat后台日志发现大量异常

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection  

时间一长tomcat就无法继续处理其他请求,从假死变成真死了。
linux运行:

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

发现CLOSE_WAIT的数量始终在400以上,一直没降过。

问题分析:
一开始我对我的HttpClient使用过程深信不疑,我不认为异常是来自这里。
所以我开始从TCP的连接状态入手,猜测可能导致异常的原因。以前经常遇到TIME_WAIT数过大导致的服务器异常,很容易解决,修改下sysctl就ok了。但是这次是CLOSE_WAIT,是完全不同的概念了。
关于TIME_WAIT和CLOSE_WAIT的区别和异常处理我会单独起一篇文章详细说说我的理解。

简单来说CLOSE_WAIT数目过大是由于被动关闭连接处理不当导致的。
我说一个场景,服务器A会去请求服务器B上面的apache获取文件资源,正常情况下,如果请求成功,那么在抓取完资源后服务器A会主动发出关闭连接的请求,这个时候就是主动关闭连接,连接状态我们可以看到是TIME_WAIT。如果一旦发生异常呢?假设请求的资源服务器B上并不存在,那么这个时候就会由服务器B发出关闭连接的请求,服务器A就是被动的关闭了连接,如果服务器A被动关闭连接之后自己并没有释放连接,那就会造成CLOSE_WAIT的状态了。
所以很明显,问题还是处在程序里头。

先看看我的HttpConnectionManager实现:

public class HttpConnectionManager { 

    private static HttpParams httpParams;
private static ClientConnectionManager connectionManager; /**
* 最大连接数
*/
public final static int MAX_TOTAL_CONNECTIONS = 800;
/**
* 获取连接的最大等待时间
*/
public final static int WAIT_TIMEOUT = 60000;
/**
* 每个路由最大连接数
*/
public final static int MAX_ROUTE_CONNECTIONS = 400;
/**
* 连接超时时间
*/
public final static int CONNECT_TIMEOUT = 10000;
/**
* 读取超时时间
*/
public final static int READ_TIMEOUT = 10000; static {
httpParams = new BasicHttpParams();
// 设置最大连接数
ConnManagerParams.setMaxTotalConnections(httpParams, MAX_TOTAL_CONNECTIONS);
// 设置获取连接的最大等待时间
ConnManagerParams.setTimeout(httpParams, WAIT_TIMEOUT);
// 设置每个路由最大连接数
ConnPerRouteBean connPerRoute = new ConnPerRouteBean(MAX_ROUTE_CONNECTIONS);
ConnManagerParams.setMaxConnectionsPerRoute(httpParams,connPerRoute);
// 设置连接超时时间
HttpConnectionParams.setConnectionTimeout(httpParams, CONNECT_TIMEOUT);
// 设置读取超时时间
HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT); SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); connectionManager = new ThreadSafeClientConnManager(httpParams, registry);
} public static HttpClient getHttpClient() {
return new DefaultHttpClient(connectionManager, httpParams);
} }

看到没MAX_ROUTE_CONNECTIONS 正好是400,跟CLOSE_WAIT非常接近啊,难道是巧合?继续往下看。
然后看看调用它的代码是什么样的:

public static String readNet (String urlPath)
{
StringBuffer sb = new StringBuffer ();
HttpClient client = null;
InputStream in = null;
InputStreamReader isr = null;
try
{
client = HttpConnectionManager.getHttpClient();
HttpGet get = new HttpGet();
get.setURI(new URI(urlPath));
HttpResponse response = client.execute(get);
if (response.getStatusLine ().getStatusCode () != 200) {
return null;
}
HttpEntity entity =response.getEntity(); if( entity != null ){
in = entity.getContent();
.....
}
return sb.toString (); }
catch (Exception e)
{
e.printStackTrace ();
return null;
}
finally
{
if (isr != null){
try
{
isr.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}
if (in != null){
try
{
<span style="color:#ff0000;">in.close ();</span>
}
catch (IOException e)
{
e.printStackTrace ();
}
}
}
}

很简单,就是个远程读取中文页面的方法。值得注意的是这一段代码是后来某某同学加上去的,看上去没啥问题,是用于非200状态的异常处理:

if (response.getStatusLine ().getStatusCode () != 200) {
return null;
}

代码本身没有问题,但是问题是放错了位置。如果这么写的话就没问题:

client = HttpConnectionManager.getHttpClient();
HttpGet get = new HttpGet();
get.setURI(new URI(urlPath));
HttpResponse response = client.execute(get); HttpEntity entity =response.getEntity(); if( entity != null ){
in = entity.getContent();
..........
} if (response.getStatusLine ().getStatusCode () != 200) {
return null;
}
return sb.toString ();

看出毛病了吧。在这篇入门(HttpClient4.X 升级 入门 + http连接池使用)里头我提到了HttpClient4使用我们常用的InputStream.close()来确认连接关闭,前面那种写法InputStream in 根本就不会被赋值,意味着一旦出现非200的连接,这个连接将永远僵死在连接池里头,太恐怖了。。。所以我们看到CLOST_WAIT数目为400,因为对一个路由的连接已经完全被僵死连接占满了。。。
其实上面那段代码还有一个没处理好的地方,异常处理不够严谨,所以最后我把代码改成了这样:

public static String readNet (String urlPath)
{
StringBuffer sb = new StringBuffer ();
HttpClient client = null;
InputStream in = null;
InputStreamReader isr = null;
HttpGet get = new HttpGet();
try
{
client = HttpConnectionManager.getHttpClient();
get.setURI(new URI(urlPath));
HttpResponse response = client.execute(get);
if (response.getStatusLine ().getStatusCode () != 200) {
get.abort();
return null;
}
HttpEntity entity =response.getEntity(); if( entity != null ){
in = entity.getContent();
......
}
return sb.toString (); }
catch (Exception e)
{
get.abort();
e.printStackTrace ();
return null;
}
finally
{
if (isr != null){
try
{
isr.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}
if (in != null){
try
{
in.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}
}
}

显示调用HttpGet的abort,这样就会直接中止这次连接,我们在遇到异常的时候应该显示调用,因为谁能保证异常是在InputStream in赋值之后才抛出的呢。

好了 ,分析完毕,明天准备总结下CLOSE_WAIT和TIME_WAIT的区别。

HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查  http://blog.csdn.net/shootyou/article/details/6615051

HttpClient Timeout waiting for connection from pool 问题解决方案的更多相关文章

  1. HttpClient 的Timeout waiting for connection from pool

    Timeout waiting for connection from pool 异常 httpClient大家用到地方会很多,先简单描述一下几个关键配置的意义 httpClient版本为4.5.1 ...

  2. HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查

    转自: http://blog.csdn.net/shootyou/article/details/6615051 今天解决了一个HttpClient的异常,汗啊,一个HttpClient使用稍有不慎 ...

  3. net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting head

    使用docker 拉镜像的时候,出现下面的错误: net/http: request canceled while waiting for connection (Client.Timeout exc ...

  4. Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

    docker pull nginx 遇到这个问题 Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: ...

  5. HttpClient Timeout

    1. Overview This tutorial will show how to configure a timeout with the Apache HttpClient 4. If you ...

  6. jedis异常:NoSuchElementException: Timeout waiting for idle object

    项目发现网络环境:java.util.NoSuchElementException: Timeout waiting for idle object 问题原因:不应该在try中释放资源.而应该在fin ...

  7. Eclipse项目导入Android Stuio 配置出现 Timeout waiting to lock buildscript class cache for build file 'H:\studioproject\Generic_SN\build.gradle'

     Eclipse项目导入Android Stuio 配置出现 Error:Timeout waiting to lock buildscript class cache for build file  ...

  8. weblogic报错----Received exception while creating connection for pool "TDMSKD": The Network Adapter could not establish the connection

    <2017-8-16 上午08时58分37秒 CST> <Info> <WebLogicServer> <BEA-000377> <Startin ...

  9. 【我的Android进阶之旅】解决AndroidStudio编译时报错:Timeout waiting to lock artifact cache .

    1. 错误描述 今天在Android Studio中,使用gradle命令的时候,出现了如下所示的错误: D:\GitLab Source\XTCLint>gradlew clean uploa ...

随机推荐

  1. 6E - 寒冰王座

    不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前. 死亡骑士:"我要买道具!" ...

  2. js常用判断和语法

    1.js获取选中的redio元素 var version = $('.version input[name="input1"]:checked').val();//单选框默认选中& ...

  3. redis使用规范文档 20170522版

    运维redis很久了,一直是口头给rd说各种要求,尝试把这些规范总结成文档 摘选一些可能比较通用的规则如下: 强制:所有的key设置过期时间(最长可设置过期时间10天,如有特殊要求,联系dba说明原因 ...

  4. 学习blinker

    from blinker import signal do_sth = signal('do_sth') #创建信号 def process(f, a, b, **kwargs): f(a, b, * ...

  5. # 2019-2020.3 《java程序设计》第一周学习总结

    2019-2020-3 <Java 程序设计>第一周学习总结 在本周的学习中,学习到了好多也收获了好多,从最基础的安装虚拟机开始,根据老师的博客中的教程一步一步的进行,在这过程中也遇到了好 ...

  6. 从中央仓库下载所想要的jar包

    中央仓库地址:https://mvnrepository.com/ 这边我搜索一个commons-logging包作为例子: 点击下面第二个绿色的comons-logging进入这个页面: 一.win ...

  7. 1.8 新特性之 Lambda Expressions

    Lambda expressions are allowed only at source level 1.8 or above The target type of this expression ...

  8. bzoj4555(多项式求逆解法)

    //和以前写的fft不太一样,可能是因为要取模?? #include<iostream> #include<cstring> #include<cmath> #in ...

  9. Codeforces791 B. Bear and Friendship Condition

    B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...

  10. SSD磁盘测试不达标排查

     最近购买了一块4T的Inter_SSD_D3-4510硬盘安装在了一台DELL PowerEdge R640服务器,经过测试发现磁盘和产品手册上描述的性能相差过大,相当于产品手册性能的1/2,一下是 ...