天天看點

HttpClient 大量連接配接等待異常的處理

前幾天要掃一批連接配接,因為涉及到ua的适配測試,是以選用了httpclient處理。

httpclient其實很好用,也用過幾次,還曾經寫過自動刷人人網人氣的程式,不過對于它掌握還是處在比較淺顯的時代

我這人比較懶,學習動力還是太弱,不逼到份上都懶得學習哎!

這次又用到這東西了就直接看了看api 就開始寫了,不過寫的時候發現一個問題。

開始跑100多個url的時候是沒問題的,不過過了一定量數後,會大量的抛出如下異常

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:417)
at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:300)
at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:224)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at com.ua.engine.Engine.httpService(Engine.java:97)
at com.ua.engine.Engine.main(Engine.java:27)
           

對此很不解,還是了解的不夠多啊,後來發現問題了,原來之前一直使用連接配接池中的一條連接配接進行連結通路,用的多了,連接配接到期,會造成連接配接等待的情況,

因為使用的HttpClient4 ,于是查了下資料,發現使用完每個連接配接後一定要及時的釋放掉,不然上面的錯會無限的報下去。

而釋放連接配接隻用InputStream 就能輕松搞定了

最後整體的代碼如下

HttpClient 大量連接配接等待異常的處理
package com.ua.engine;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;




public class Engine {
    
    public static void main(String[] args) {
        try {
            HashMap<String,List<String>> map = readURL();
            BufferedWriter bw = new BufferedWriter(new PrintWriter("D://furl/log.txt"));
                List<String> weblist = map.get("web");
                int i = 1;
                for(String url :weblist){
                    System.out.println(i);
                    String temp = Engine.httpService("http://52.test.com"+url,"web")+" "+url;
                    System.out.println(temp);
                    bw.write(temp);
                    bw.write("\r\n");
                    i++;
                }
                List<String> touchlist = map.get("touch");
                for(String url :touchlist){
                    System.out.println(i);
                    String temp = Engine.httpService("http://52.test.com"+url,"touch")+" "+url;
                    System.out.println(temp);
                    bw.write(temp);
                    bw.write("\r\n");
                    i++;
                }
                List<String> hdlist = map.get("hd");
                for(String url :hdlist){
                    System.out.println(i);
                    String temp = Engine.httpService("http://52.test.com"+url,"hd")+" "+url;
                    System.out.println(temp);
                    bw.write(temp);
                    bw.write("\r\n");
                    i++;
                }
                List<String> cslist = map.get("cs");
                for(String url :cslist){
                    System.out.println(i);
                    String temp = Engine.httpService("http://52.test.com"+url,"cs")+" "+url;
                    System.out.println(temp);
                    bw.write(temp);
                    bw.write("\r\n");
                    i++;
                }
                bw.flush();
                bw.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static HttpClient httpclient = null;
    static {
        HttpClientUtils hcu = new HttpClientUtils();
        hcu.setConnTimeout(10000);
        hcu.setSoTimeout(10000);
        hcu.setBufSize(128 * 1024);
        hcu.setAgent("");
        hcu.setMaxRedirect(3);
        hcu.setMaxTotal(500);
        hcu.setMaxPerRoute(100);
        httpclient = hcu.newClient();
    }
    public static String httpService(String url,String version) {
        HttpGet get = new HttpGet(url);
        if("web".equals(version)){
            get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5");
        }else if("touch".equals(version)){
            get.setHeader("User-Agent", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
        }else if("hd".equals(version)){
            get.setHeader("User-Agent", "Mozilla/5.0 (iPad; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
        }else {
            get.setHeader("User-Agent", "lg-kp500 teleca/wap2.0 midp-2.0/cldc-1.1 untrusted/1.0 nokia7610 (3.0417.0ch) symbianos/7.0s series60/2.1 profile/midp-2.0 conf");
        }


//        HttpEntity entity = response.getEntity();
//        System.out.println(EntityUtils.toString(entity));
        HttpResponse response = null;
        String code = "error "+url;
        InputStream in = null;  
        try {
            response = httpclient.execute(get);
            HttpEntity entity =response.getEntity();  
            if( entity != null ){   
             in = entity.getContent(); //之前沒使用這個造成了大量異常抛出,隻要是
            }
            code = response.getStatusLine().getStatusCode()+" "+version+" "+url;
            return code;
        }
        catch (Exception e) {
            System.out.println(code);
            e.printStackTrace();
        }finally {
            if(in!=null){
                try {
                    in.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return code;
    }
    
    public static HashMap<String,List<String>> readURL() throws Exception{
        List<String> weblist = new ArrayList<String>();
        List<String> touchlist = new ArrayList<String>();
        List<String> hdlist = new ArrayList<String>();
        List<String> cslist = new ArrayList<String>();
        
        BufferedReader br = new BufferedReader(new FileReader("D://furl/1.txt"));
        while(br.ready()){
            String url = br.readLine();
            if(url.toLowerCase().indexOf(".html")>-1){
                weblist.add(url);
            }else if(url.toLowerCase().indexOf("touch")>-1){
                touchlist.add(url);
            }else if(url.toLowerCase().indexOf("/hd/")>-1){
                hdlist.add(url);
            }else {
                cslist.add(url);
            }
        }
        br = new BufferedReader(new FileReader("D://furl/url1.txt"));
        while(br.ready()){
            String url = br.readLine();
            if(url.toLowerCase().indexOf(".html")>-1){
                weblist.add(url);
            }else if(url.toLowerCase().indexOf("touch")>-1){
                touchlist.add(url);
            }else if(url.toLowerCase().indexOf("/hd/")>-1){
                hdlist.add(url);
            }else {
                cslist.add(url);
            }
        }test
        br = new BufferedReader(new FileReader("D://furl/urljian1.txt"));
        while(br.ready()){
            String url = br.readLine();
            if(url.toLowerCase().indexOf(".html")>-1){
                weblist.add(url);
            }else if(url.toLowerCase().indexOf("touch")>-1){
                touchlist.add(url);
            }else if(url.toLowerCase().indexOf("/hd/")>-1){
                hdlist.add(url);
            }else {
                cslist.add(url);
            }
        }
        HashMap<String,List<String>> map = new HashMap<String,List<String>>();
        System.out.println(weblist.size());
        System.out.println(touchlist.size());
        System.out.println(hdlist.size());
        System.out.println(cslist.size());
        map.put("web", weblist);
        map.put("touch", touchlist);
        map.put("hd", hdlist);
        map.put("cs", cslist);
        return map;
    }
}                
package com.ua.engine;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;

public class HttpClientUtils {
    private int connTimeout = 5000;
    private int soTimeout = 5000;
    private int bufSize = 128 * 1024;
    private String agent = "";
    private int maxRedirect = 3;
    private int maxTotal = 500;
    private int maxPerRoute = 100;

    public HttpClient newClient() {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                this.connTimeout);
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, this.soTimeout);
        params.setParameter(CoreConnectionPNames.SO_REUSEADDR, true);
        params.setParameter(CoreConnectionPNames.TCP_NODELAY, false);
        params.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
                this.bufSize);
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
                HttpVersion.HTTP_1_1);
        params.setParameter(CoreProtocolPNames.USER_AGENT, this.agent);
        params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME,
                "org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager");
        params.setParameter(ClientPNames.MAX_REDIRECTS, this.maxRedirect);
        ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager();
        ccm.setMaxTotal(this.maxTotal);
        ccm.setDefaultMaxPerRoute(this.maxPerRoute);
        return new DefaultHttpClient(ccm, params);
    }

    public long getConnTimeout() {
        return connTimeout;
    }

    public void setConnTimeout(int connTimeout) {
        this.connTimeout = connTimeout;
    }

    public long getSoTimeout() {
        return soTimeout;
    }

    public void setSoTimeout(int soTimeout) {
        this.soTimeout = soTimeout;
    }

    public int getBufSize() {
        return bufSize;
    }

    public void setBufSize(int bufSize) {
        this.bufSize = bufSize;
    }

    public String getAgent() {
        return agent;
    }

    public void setAgent(String agent) {
        this.agent = agent;
    }

    public int getMaxRedirect() {
        return maxRedirect;
    }

    public void setMaxRedirect(int maxRedirect) {
        this.maxRedirect = maxRedirect;
    }

    public int getMaxTotal() {
        return maxTotal;
    }

    public void setMaxTotal(int maxTotal) {
        this.maxTotal = maxTotal;
    }

    public int getMaxPerRoute() {
        return maxPerRoute;
    }

    public void setMaxPerRoute(int maxPerRoute) {
        this.maxPerRoute = maxPerRoute;
    }
}                

版權聲明:本文為CSDN部落客「weixin_34235105」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_34235105/article/details/91896588