当前位置: 首页 > Java > 正文

httpclient timeout waiting for connection

关键字:
1 星2 星3 星4 星5 星 (暂无评分)
Loading ... Loading ...
baidu_share

最近在线上发现了httpclient超时异常,起初以为是网络问题,最好发现是代码问题。

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)

造成问题主要原因是:httpclient 未对execute方法做捕获(在上一层做cache),未对失败情况做处理,未对失败连接进行回收,造成大量CLOSE_WAIT的状态。

try {
			responseBody = httpClient.execute(httpGet, responseHandler);
		} catch (IOException e) {
			logger.error("调用httpclient获取info时出错!param=" + url + param	+ " ;", e);
			httpGet.abort();
			return null;
		}

应该调用abort方法关闭失败连接。

        private PoolingClientConnectionManager cm;
 
//http连接池初始化
	public void init() {
		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory
				.getSocketFactory()));
		cm = new PoolingClientConnectionManager(schemeRegistry);
		cm.setMaxTotal(500);
		cm.setDefaultMaxPerRoute(300);
	}

本文固定链接: http://www.chepoo.com/httpclient-timeout-waiting-for-connection.html | IT技术精华网

httpclient timeout waiting for connection:等您坐沙发呢!

发表评论