List of usage examples for org.apache.http.impl.client IdleConnectionEvictor start
public void start()
From source file:com.lxf.spider.client.ClientEvictExpiredConnections.java
public static void main(String[] args) throws Exception { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100);/*from w w w . j a v a 2 s .c o m*/ CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); try { // create an array of URIs to perform GETs on String[] urisToGet = { "http://hc.apache.org/", "http://hc.apache.org/httpcomponents-core-ga/", "http://hc.apache.org/httpcomponents-client-ga/", }; IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm); connEvictor.start(); for (int i = 0; i < urisToGet.length; i++) { String requestURI = urisToGet[i]; HttpGet request = new HttpGet(requestURI); System.out.println("Executing request " + requestURI); CloseableHttpResponse response = httpclient.execute(request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } // Sleep 10 sec and let the connection evictor do its job Thread.sleep(20000); // Shut down the evictor thread connEvictor.shutdown(); connEvictor.join(); } finally { httpclient.close(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientEvictExpiredConnections.java
public static void main(String[] args) throws Exception { PoolingClientConnectionManager cm = new PoolingClientConnectionManager(); cm.setMaxTotal(100);/*from ww w. j a v a 2s. com*/ HttpClient httpclient = new DefaultHttpClient(cm); try { // create an array of URIs to perform GETs on String[] urisToGet = { "http://jakarta.apache.org/", "http://jakarta.apache.org/commons/", "http://jakarta.apache.org/commons/httpclient/", "http://svn.apache.org/viewvc/jakarta/httpcomponents/" }; IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm); connEvictor.start(); for (int i = 0; i < urisToGet.length; i++) { String requestURI = urisToGet[i]; HttpGet req = new HttpGet(requestURI); System.out.println("executing request " + requestURI); HttpResponse rsp = httpclient.execute(req); HttpEntity entity = rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } System.out.println("----------------------------------------"); EntityUtils.consume(entity); } // Sleep 10 sec and let the connection evictor do its job Thread.sleep(20000); // Shut down the evictor thread connEvictor.shutdown(); connEvictor.join(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:httpclient.client.ClientEvictExpiredConnections.java
public static void main(String[] args) throws Exception { // Create and initialize HTTP parameters HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, 100); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); HttpClient httpclient = new DefaultHttpClient(cm, params); // create an array of URIs to perform GETs on String[] urisToGet = { "http://jakarta.apache.org/", "http://jakarta.apache.org/commons/", "http://jakarta.apache.org/commons/httpclient/", "http://svn.apache.org/viewvc/jakarta/httpcomponents/" }; IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm); connEvictor.start(); for (int i = 0; i < urisToGet.length; i++) { String requestURI = urisToGet[i]; HttpGet req = new HttpGet(requestURI); System.out.println("executing request " + requestURI); HttpResponse rsp = httpclient.execute(req); HttpEntity entity = rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }//w w w . j a v a2 s . co m System.out.println("----------------------------------------"); if (entity != null) { entity.consumeContent(); } } // Sleep 10 sec and let the connection evictor do its job Thread.sleep(20000); // Shut down the evictor thread connEvictor.shutdown(); connEvictor.join(); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }