HttpClient链接池抛出大量ConnectionPoolTimeoutException

今天解决了一个HttpClient的异常,汗啊,一个HttpClient使用稍有不慎都会是毁灭级别的啊。java

这里有以前由于route配置不当致使服务器异常的一个处理:http://blog.csdn.net/shootyou/article/details/6415248linux

里面的HttpConnectionManager实现就是我在这里使用的实现。apache

 

问题表现:tomcat

tomcat后台日志发现大量异常服务器

 

[plain] view plain copyapp

 print?tcp

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


时间一长tomcat就没法继续处理其余请求,从假死变成真死了。url

 

linux运行:spa

[plain] view plain copy.net

 print?

  1. 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实现:

 

[java] view plain copy

 print?

  1. public class HttpConnectionManager {   
  2.   
  3.     private static HttpParams httpParams;  
  4.     private static ClientConnectionManager connectionManager;  
  5.   
  6.     /** 
  7.      * 最大链接数 
  8.      */  
  9.     public final static int MAX_TOTAL_CONNECTIONS = 800;  
  10.     /** 
  11.      * 获取链接的最大等待时间 
  12.      */  
  13.     public final static int WAIT_TIMEOUT = 60000;  
  14.     /** 
  15.      * 每一个路由最大链接数 
  16.      */  
  17.     public final static int MAX_ROUTE_CONNECTIONS = 400;  
  18.     /** 
  19.      * 链接超时时间 
  20.      */  
  21.     public final static int CONNECT_TIMEOUT = 10000;  
  22.     /** 
  23.      * 读取超时时间 
  24.      */  
  25.     public final static int READ_TIMEOUT = 10000;  
  26.   
  27.     static {  
  28.         httpParams = new BasicHttpParams();  
  29.         // 设置最大链接数  
  30.         ConnManagerParams.setMaxTotalConnections(httpParams, MAX_TOTAL_CONNECTIONS);  
  31.         // 设置获取链接的最大等待时间  
  32.         ConnManagerParams.setTimeout(httpParams, WAIT_TIMEOUT);  
  33.         // 设置每一个路由最大链接数  
  34.         ConnPerRouteBean connPerRoute = new ConnPerRouteBean(MAX_ROUTE_CONNECTIONS);  
  35.         ConnManagerParams.setMaxConnectionsPerRoute(httpParams,connPerRoute);  
  36.         // 设置链接超时时间  
  37.         HttpConnectionParams.setConnectionTimeout(httpParams, CONNECT_TIMEOUT);  
  38.         // 设置读取超时时间  
  39.         HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT);  
  40.   
  41.         SchemeRegistry registry = new SchemeRegistry();  
  42.         registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));  
  43.         registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));  
  44.   
  45.         connectionManager = new ThreadSafeClientConnManager(httpParams, registry);  
  46.     }  
  47.   
  48.     public static HttpClient getHttpClient() {  
  49.         return new DefaultHttpClient(connectionManager, httpParams);  
  50.     }  
  51.   
  52. }  

 

 

看到没MAX_ROUTE_CONNECTIONS 正好是400,跟CLOSE_WAIT很是接近啊,难道是巧合?继续往下看。

而后看看调用它的代码是什么样的:

 

[java] view plain copy

 print?

  1. public static String readNet (String urlPath)  
  2.     {  
  3.         StringBuffer sb = new StringBuffer ();  
  4.         HttpClient client = null;  
  5.         InputStream in = null;  
  6.         InputStreamReader isr = null;  
  7.         try  
  8.         {  
  9.             client = HttpConnectionManager.getHttpClient();  
  10.             HttpGet get = new HttpGet();  
  11.             get.setURI(new URI(urlPath));  
  12.             HttpResponse response = client.execute(get);  
  13.             if (response.getStatusLine ().getStatusCode () != 200) {  
  14.                 return null;  
  15.             }  
  16.             HttpEntity entity =response.getEntity();  
  17.               
  18.             if( entity != null ){  
  19.                 in = entity.getContent();  
  20.                 .....  
  21.             }  
  22.             return sb.toString ();  
  23.               
  24.         }  
  25.         catch (Exception e)  
  26.         {  
  27.             e.printStackTrace ();  
  28.             return null;  
  29.         }  
  30.         finally  
  31.         {  
  32.             if (isr != null){  
  33.                 try  
  34.                 {  
  35.                     isr.close ();  
  36.                 }  
  37.                 catch (IOException e)  
  38.                 {  
  39.                     e.printStackTrace ();  
  40.                 }  
  41.             }  
  42.             if (in != null){  
  43.                 try  
  44.                 {  
  45.                     <span style="color:#ff0000;">in.close ();</span>  
  46.                 }  
  47.                 catch (IOException e)  
  48.                 {  
  49.                     e.printStackTrace ();  
  50.                 }  
  51.             }  
  52.         }  
  53.     }  


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

 

 

[java] view plain copy

 print?

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


代码自己没有问题,可是问题是放错了位置。若是这么写的话就没问题:

 

 

[java] view plain copy

 print?

  1. client = HttpConnectionManager.getHttpClient();  
  2.             HttpGet get = new HttpGet();  
  3.             get.setURI(new URI(urlPath));  
  4.             HttpResponse response = client.execute(get);  
  5.               
  6.             HttpEntity entity =response.getEntity();  
  7.               
  8.             if( entity != null ){  
  9.                 in = entity.getContent();  
  10.             ..........  
  11.             }  
  12.               
  13.             if (response.getStatusLine ().getStatusCode () != 200) {  
  14.                 return null;  
  15.             }  
  16.             return sb.toString ();  

看出毛病了吧。在这篇入门(HttpClient4.X 升级 入门 + http链接池使用)里头我提到了HttpClient4使用咱们经常使用的InputStream.close()来确认链接关闭,前面那种写法InputStream in 根本就不会被赋值,意味着一旦出现非200的链接,这个链接将永远僵死在链接池里头,太恐怖了。。。因此咱们看到CLOST_WAIT数目为400,由于对一个路由的链接已经彻底被僵死链接占满了。。。

 

其实上面那段代码还有一个没处理好的地方,异常处理不够严谨,因此最后我把代码改为了这样:

 

[java] view plain copy

 print?

  1. public static String readNet (String urlPath)  
  2.     {  
  3.         StringBuffer sb = new StringBuffer ();  
  4.         HttpClient client = null;  
  5.         InputStream in = null;  
  6.         InputStreamReader isr = null;  
  7.         HttpGet get = new HttpGet();  
  8.         try  
  9.         {  
  10.             client = HttpConnectionManager.getHttpClient();  
  11.             get.setURI(new URI(urlPath));  
  12.             HttpResponse response = client.execute(get);  
  13.             if (response.getStatusLine ().getStatusCode () != 200) {  
  14.                 get.abort();  
  15.                 return null;  
  16.             }  
  17.             HttpEntity entity =response.getEntity();  
  18.               
  19.             if( entity != null ){  
  20.                 in = entity.getContent();  
  21.                 ......  
  22.             }  
  23.             return sb.toString ();  
  24.               
  25.         }  
  26.         catch (Exception e)  
  27.         {  
  28.             get.abort();  
  29.             e.printStackTrace ();  
  30.             return null;  
  31.         }  
  32.         finally  
  33.         {  
  34.             if (isr != null){  
  35.                 try  
  36.                 {  
  37.                     isr.close ();  
  38.                 }  
  39.                 catch (IOException e)  
  40.                 {  
  41.                     e.printStackTrace ();  
  42.                 }  
  43.             }  
  44.             if (in != null){  
  45.                 try  
  46.                 {  
  47.                     in.close ();  
  48.                 }  
  49.                 catch (IOException e)  
  50.                 {  
  51.                     e.printStackTrace ();  
  52.                 }  
  53.             }  
  54.         }  
  55.     }  

 

 

显示调用HttpGet的abort,这样就会直接停止此次链接,咱们在遇到异常的时候应该显示调用,由于谁能保证异常是在InputStream in赋值以后才抛出的呢。

 

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

相关文章
相关标签/搜索