预防httpClient连接数满的方法

由于自己做的项目最近报了很多这个异常:org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection

所以研究了一下连接这方面的资料,总结以下几点:

httpClient 预防连接满了的方法:

1、设置最大连接数 
    static {  
        httpParams = new BasicHttpParams();  
        // 设置最大连接数  
        ConnManagerParams.setMaxTotalConnections(httpParams, 600);  
        // 设置获取连接的最大等待时间  
        ConnManagerParams.setTimeout(httpParams, 60000);  
        // 设置每个路由最大连接数  
        ConnPerRouteBean connPerRoute = new ConnPerRouteBean(300);  
        ConnManagerParams.setMaxConnectionsPerRoute(httpParams,connPerRoute);  
        // 设置连接超时时间  
        HttpConnectionParams.setConnectionTimeout(httpParams, 10000);  
        // 设置读取超时时间  
        HttpConnectionParams.setSoTimeout(httpParams, 10000);  
        
2、每个请求之后关闭连接
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 ();  
                }  
            }  
        }  
    }          
        
3、设置tomcat的连接数
1、修改最大连接数,在/opt/probe/patrol/tomcat_plan_5535/conf/server.xml中
<Connector port="80" maxHttpHeaderSize="8192"
               maxThreads="600" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"

               connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />        


参考资料:

http://www.haogongju.net/art/1928653  

HttpClient连接池原理及一次连接时序图

http://blog.csdn.net/shootyou/article/details/6615051

http://my.eoe.cn/530696/archive/10540.html

HttpClient详解(二)

http://www.2cto.com/os/201303/193008.html

Linux下查看tcp连接数及状态

设置tomcat的连接数

http://www.blogjava.net/wangxinsh55/archive/2012/07/16/383209.html  --这边文章讲解了HTTPClient4的连接机制,很清晰

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值