Java 连接池上的Httpclient 4.3阻塞

Java 连接池上的Httpclient 4.3阻塞,java,httpclient,Java,Httpclient,当我使用httpclient 4.3时,如下所示 static { try { SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory

当我使用httpclient 4.3时,如下所示

static {
    try {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());

        CookieSpecProvider easySpecProvider = new CookieSpecProvider() {

            public CookieSpec create(HttpContext context) {
                return new BrowserCompatSpec() {
                    @Override
                    public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException {
                        // Oh, I am easy
                    }
                };
            }

        };
        Registry<CookieSpecProvider> r = RegistryBuilder.<CookieSpecProvider> create()
                .register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
                .register(CookieSpecs.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory())
                .register("easy", easySpecProvider).build();
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000)
                .setSocketTimeout(10000).setConnectTimeout(10000).setCookieSpec("easy").setRedirectsEnabled(false)
                .build();

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(100);
        cm.setDefaultMaxPerRoute(10);

        client = HttpClients.custom().setConnectionManager(cm).setDefaultCookieSpecRegistry(r)
                .setSSLSocketFactory(sslsf).setDefaultRequestConfig(requestConfig).build();
    } catch (Exception e) {
        logger.error("http client init fail!", e);
    }
}

public static String execute(HttpRequest httpRequest) {
    CloseableHttpResponse response = null;
    HttpGet httpGet = null;
    HttpEntity httpEntity = null;

    try {
        httpGet = new HttpGet(httpRequest.getUrl());

        httpGet.setHeader("Connection", "close"); 
        if (httpRequest.isUseGzip()) {
            httpGet.addHeader("Accept-Encoding", "gzip,deflate,sdch");
        }
        if (!StringUtils.isEmpty(httpRequest.getContentType())) {
            httpRequest.setContentType(httpRequest.getContentType());
        }
        httpGet.addHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63");

        response = client.execute(httpGet);
        httpEntity = response.getEntity();

        byte[] bytes = null;
        try {
            bytes = EntityUtils.toByteArray(httpEntity);
        } catch (Exception e) {
            return null;
        }

        if (response.getStatusLine().getStatusCode() != 200) {
            logger.warn("error! StatusCode: " + response.getStatusLine().getStatusCode() + ", url: "
                    + httpRequest.getUrl());
            return null;
        }

        @SuppressWarnings("deprecation")
        String charset = EntityUtils.getContentCharSet(httpEntity);
        if (StringUtils.isEmpty(charset)) {
            Matcher match = charsetPatterm.matcher(new String(bytes));

            if (match.find()) {
                charset = match.group(1);
            }
        }
        if (!StringUtils.isEmpty(charset)) {
            String strUtf8 = new String(new String(bytes, charset).getBytes(), GlobalConfig.ENCODING);

            return StringEscapeUtils.unescapeHtml4(strUtf8);
        }
    } catch (Exception e) {
        logger.error("error! url [" + httpRequest.getUrl() + "]", e);
    } finally {
        try {
            if (httpEntity != null) {
                EntityUtils.consume(httpEntity);
            }
            if (response != null) {
                response.close();
            }
            if (httpGet != null) {
                httpGet.abort();
            }
        } catch (Exception e) {
            // ignore
        }
    }

    return null;
}
静态{
试一试{
SSLContextBuilder=新的SSLContextBuilder();
loadTrustMaterial(空,新TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf=新的SSLConnectionSocketFactory(builder.build());
CookieSpecProvider easySpecProvider=新CookieSpecProvider(){
公共CookieSpec创建(HttpContext上下文){
返回新的BrowserCompatSpec(){
@凌驾
public void validate(Cookie Cookie,CookieOrigin origin)引发格式错误的CookieException{
//哦,我很容易
}
};
}
};
注册表r=RegistryBuilder.create()
.register(CookiesSpecs.BEST_MATCH,新的BestMatchSpecFactory())
.register(CookieSpecs.BROWSER_兼容性,新浏览器CompatisSpecFactory())
.register(“easy”,easySpecProvider).build();
RequestConfig RequestConfig=RequestConfig.custom().setConnectionRequestTimeout(5000)
.setSocketTimeout(10000).setConnectTimeout(10000).setCookieSpec(“容易”).setRedirectsEnabled(false)
.build();
PoolightPClientConnectionManager cm=新的PoolightPClientConnectionManager();
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(10);
client=HttpClients.custom().setConnectionManager(cm).setDefaultCookieSpecRegistry(r)
.setsslssocketfactory(sslsf).setDefaultRequestConfig(requestConfig).build();
}捕获(例外e){
错误(“http客户端初始化失败!”,e);
}
}
公共静态字符串执行(HttpRequest HttpRequest){
CloseableHttpResponse响应=null;
HttpGet-HttpGet=null;
HttpEntity HttpEntity=null;
试一试{
httpGet=newhttpget(httpRequest.getUrl());
setHeader(“连接”,“关闭”);
if(httpRequest.isUseGzip()){
addHeader(“接受编码”、“gzip、deflate、sdch”);
}
如果(!StringUtils.isEmpty(httpRequest.getContentType())){
httpRequest.setContentType(httpRequest.getContentType());
}
httpGet.addHeader(“用户代理”,
“Mozilla/5.0(Windows NT 6.1)AppleWebKit/537.36(KHTML,像Gecko)Chrome/31.0.1650.63”);
response=client.execute(httpGet);
httpEntity=response.getEntity();
字节[]字节=null;
试一试{
字节=EntityUtils.toByteArray(httpEntity);
}捕获(例外e){
返回null;
}
如果(response.getStatusLine().getStatusCode()!=200){
logger.warn(“错误!状态代码:+response.getStatusLine().getStatusCode()+”,url:“
+httpRequest.getUrl());
返回null;
}
@抑制警告(“弃用”)
字符串字符集=EntityUtils.getContentCharSet(httpEntity);
if(StringUtils.isEmpty(字符集)){
Matcher match=charsetPatterm.Matcher(新字符串(字节));
if(match.find()){
字符集=匹配组(1);
}
}
如果(!StringUtils.isEmpty(字符集)){
String strUtf8=新字符串(新字符串(字节,字符集).getBytes(),GlobalConfig.ENCODING);
返回StringEscapeUtils.unescapethml4(strUtf8);
}
}捕获(例外e){
error(“error!url[“+httpRequest.getUrl()+”],e);
}最后{
试一试{
if(httpEntity!=null){
EntityUtils.consume(httpEntity);
}
if(响应!=null){
response.close();
}
if(httpGet!=null){
httpGet.abort();
}
}捕获(例外e){
//忽略
}
}
返回null;
}
线程将阻塞。。像这样的jstack秀。我只是用它来抓取一些网站。当状态码为404时发生。
我的问题与此类似

"pool-1-thread-10" prio=10 tid=0x00007f7168003000 nid=0x3e4d waiting on condition [0x00007f717c398000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000000e69d7350> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
        at org.apache.http.pool.PoolEntryFuture.await(PoolEntryFuture.java:133)
        at org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking(AbstractConnPool.java:282)
        at org.apache.http.pool.AbstractConnPool.access$000(AbstractConnPool.java:64)
        at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:177)
        at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:170)
        at org.apache.http.pool.PoolEntryFuture.get(PoolEntryFuture.java:102)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.jav
a:244)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:231)
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:173)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
“pool-1-thread-10”优先级=10 tid=0x00007f7168003000 nid=0x3e4d等待状态[0x00007f717c398000]
java.lang.Thread.State:等待(停车)
在sun.misc.Unsafe.park(本机方法)
-停车等待(java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
位于java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
位于java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
位于org.apache.http.pool.PoolEntryFuture.await(PoolEntryFuture.java:133)
位于org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking(AbstractConnPool.java:282)
在org.apache.http.pool.AbstractConnPool.access$000(AbstractConnPool.java:64)
位于org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:177)
位于org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:170)
位于org.apache.http.pool.PoolEntryFuture.get(PoolEntryFuture.java:102)
在org.apache.http.impl.conn.poolghttpclientconnectionmanager.leaseConnection(poolghttpclientconnectionmanager.jav
a:244)
在org.apache.http.impl.conn.poolghttpclientconnectionmanager$1.get(poolghttpclientconnectionmanager.java:231)
位于org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:173)
位于org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
位于org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
位于org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
位于org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
在org.apache.http.impl.client.CloseableHttpClient.execute上(CloseableHttpClient.java:82)
在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.j
// In order to ensure correct deallocation of system resources
// the user MUST call CloseableHttpResponse#close() from a finally clause.
// Please note that if response content is not fully consumed the underlying
// connection cannot be safely re-used and will be shut down and discarded
// by the connection manager. 
try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity1 = response1.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity1);
} finally {
    response1.close();
}