dd
为了验证之前关闭连接的操作,写了如下代码:
public static void main(String[] args) throws IOException, InterruptedException { PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(100000L, TimeUnit.SECONDS);//指定一个连接的生存时间最大为100秒。 manager.setDefaultMaxPerRoute(1);//指定一个router缓存的最大的连接数为1 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000)/*从连接池中取连接的超时时间。*/.setConnectTimeout(3000)/*建立连接的*/ .setSocketTimeout(5000)/*包的最大间隔时间*/.build(); HttpClientBuilder create = HttpClientBuilder.create(); create.setConnectionManager(manager); create.setDefaultRequestConfig(requestConfig); create.evictExpiredConnections();//关闭失效的连接 create.evictIdleConnections(1000*60L*3, TimeUnit.MILLISECONDS);//关闭长时间不用的连接 final CloseableHttpClient httpClient = create.build(); final HttpGet httpGet1 = new HttpGet("http://www.baidu.com"); Thread t1 = new Thread(new Runnable() {//新线程进行连接,使用第一个连接, @Override public void run() { try { CloseableHttpResponse execute = httpClient.execute(httpGet1); StatusLine statusLine = execute.getStatusLine(); int statusCode = statusLine.getStatusCode(); if(statusCode == HttpStatus.SC_OK) { System.out.println("第一个请求成功,sleeep 3秒"); Thread.sleep(3000L); System.out.println("第一个请求醒来"); // EntityUtils.consume(execute.getEntity());//这是一个开关,第一次不注释,第二次注释掉 } else {//不是200,关闭连接 execute.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t1.start();//启动第一个线程 Thread.sleep(1000L);//睡眠1秒,保证第一个线程优先使用开辟的连接 final HttpGet httpGet2 = new HttpGet("http://www.baidu.com"); CloseableHttpResponse execute = httpClient.execute(httpGet2); StatusLine statusLine = execute.getStatusLine(); int statusCode = statusLine.getStatusCode(); if(statusCode == HttpStatus.SC_OK) { System.out.println("第二个请求成功,sleeep 3秒"); EntityUtils.consume(execute.getEntity()); } else {//不是200,关闭连接 execute.close(); } httpClient.close(); }
试验发现,当不注释掉那个EntityUtils.consume的时候是不会出现超时错误的,当注释掉之后就会出现Timeout waiting for connection from pool错误,所以可以确定,使用EntityUtils.consume的确是可以将连接还回连接池。
相关推荐
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
这里提到的四个jar包——httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5,都是Apache HttpClient库的不同组件,用于支持HTTP通信和相关功能。 **httpcore-4.2.4.jar** 是HTTP Core模块...
httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar httpclient-4.1-alpha1.jar
httpclient常用的jar包,便于大家使用
赠送jar包:httpclient-4.4.1.jar; 赠送原API文档:httpclient-4.4.1-javadoc.jar; 赠送源代码:httpclient-4.4.1-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.1.pom; 包含翻译后的API文档:httpclient...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
赠送jar包:httpclient-4.5.12.jar; 赠送原API文档:httpclient-4.5.12-javadoc.jar; 赠送源代码:httpclient-4.5.12-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.12.pom; 包含翻译后的API文档:...
用于http请求的jar包
赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...
赠送jar包:httpclient-4.5.10.jar; 赠送原API文档:httpclient-4.5.10-javadoc.jar; 赠送源代码:httpclient-4.5.10-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.10.pom; 包含翻译后的API文档:...
《Apache Commons HttpClient 3.0-rc4:深入解析与应用》 Apache Commons HttpClient 是一个广泛使用的Java库,专门用于实现HTTP客户端的功能。这个库在3.0-rc4版本中提供了许多改进和优化,使其成为开发人员进行...
赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)-英语-对照...
httpclient-4.5所需jar包,里面包含httpclient-...4.httpclient-4.5.jar 5.httpclient-cache-4.5.jar 6.httpclient-win-4.5.jar 7.httpcore-4.4.1.jar 8.httpmime-4.5.jar 9.jna-4.1.0.jar 10.jna-platform-4.1.0.jar
httpclient-4.3-beta1
赠送jar包:httpclient-4.4.jar; 赠送原API文档:httpclient-4.4-javadoc.jar; 赠送源代码:httpclient-4.4-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.pom; 包含翻译后的API文档:httpclient-4.4-...
赠送jar包:httpclient-4.2.5.jar; 赠送原API文档:httpclient-4.2.5-javadoc.jar; 赠送源代码:httpclient-4.2.5-sources.jar; 赠送Maven依赖信息文件:httpclient-4.2.5.pom; 包含翻译后的API文档:httpclient...
httpclient-4.5.13-sources.jar
在项目中集成HttpClient,通常需要将`commons-httpclient-3.1-rc1.jar`添加到项目的类路径中,然后通过创建HttpClient实例,设置必要的配置,并调用相关方法发起HTTP请求。 接下来是`commons-logging.jar`,它是...