Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * ====================================================================
  3.  *
  4.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  5.  *  contributor license agreements.  See the NOTICE file distributed with
  6.  *  this work for additional information regarding copyright ownership.
  7.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  8.  *  (the "License"); you may not use this file except in compliance with
  9.  *  the License.  You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  *  Unless required by applicable law or agreed to in writing, software
  14.  *  distributed under the License is distributed on an "AS IS" BASIS,
  15.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  *  See the License for the specific language governing permissions and
  17.  *  limitations under the License.
  18.  * ====================================================================
  19.  *
  20.  * This software consists of voluntary contributions made by many
  21.  * individuals on behalf of the Apache Software Foundation.  For more
  22.  * information on the Apache Software Foundation, please see
  23.  * <http://www.apache.org/>.
  24.  *
  25.  */
  26.  
  27. package org.apache.http.examples.client;
  28.  
  29. import java.util.concurrent.TimeUnit;
  30.  
  31. import org.apache.http.HttpEntity;
  32. import org.apache.http.HttpResponse;
  33. import org.apache.http.HttpVersion;
  34. import org.apache.http.client.HttpClient;
  35. import org.apache.http.client.methods.HttpGet;
  36. import org.apache.http.conn.ClientConnectionManager;
  37. import org.apache.http.conn.params.ConnManagerParams;
  38. import org.apache.http.conn.scheme.PlainSocketFactory;
  39. import org.apache.http.conn.scheme.Scheme;
  40. import org.apache.http.conn.scheme.SchemeRegistry;
  41. import org.apache.http.impl.client.DefaultHttpClient;
  42. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  43. import org.apache.http.params.BasicHttpParams;
  44. import org.apache.http.params.HttpParams;
  45. import org.apache.http.params.HttpProtocolParams;
  46.  
  47. /**
  48.  * Example demonstrating how to evict expired and idle connections
  49.  * from the connection pool.
  50.  */
  51. public class ClientEvictExpiredConnections {
  52.  
  53.     public static void main(String[] args) throws Exception {
  54.         // Create and initialize HTTP parameters
  55.         HttpParams params = new BasicHttpParams();
  56.         ConnManagerParams.setMaxTotalConnections(params, 100);
  57.         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  58.        
  59.         // Create and initialize scheme registry
  60.         SchemeRegistry schemeRegistry = new SchemeRegistry();
  61.         schemeRegistry.register(
  62.                 new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  63.        
  64.         ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
  65.         HttpClient httpclient = new DefaultHttpClient(cm, params);
  66.        
  67.         // create an array of URIs to perform GETs on
  68.         String[] urisToGet = {
  69.             "http://jakarta.apache.org/",
  70.             "http://jakarta.apache.org/commons/",
  71.             "http://jakarta.apache.org/commons/httpclient/",
  72.             "http://svn.apache.org/viewvc/jakarta/httpcomponents/"
  73.         };
  74.        
  75.         IdleConnectionEvictor connEvictor = new IdleConnectionEvictor(cm);
  76.         connEvictor.start();
  77.        
  78.         for (int i = 0; i < urisToGet.length; i++) {
  79.             String requestURI = urisToGet[i];
  80.             HttpGet req = new HttpGet(requestURI);
  81.  
  82.             System.out.println("executing request " + requestURI);
  83.  
  84.             HttpResponse rsp = httpclient.execute(req);
  85.             HttpEntity entity = rsp.getEntity();
  86.  
  87.             System.out.println("----------------------------------------");
  88.             System.out.println(rsp.getStatusLine());
  89.             if (entity != null) {
  90.                 System.out.println("Response content length: " + entity.getContentLength());
  91.             }
  92.             System.out.println("----------------------------------------");
  93.  
  94.             if (entity != null) {
  95.                 entity.consumeContent();
  96.             }
  97.         }
  98.        
  99.         // Sleep 10 sec and let the connection evictor do its job
  100.         Thread.sleep(20000);
  101.        
  102.         // Shut down the evictor thread
  103.         connEvictor.shutdown();
  104.         connEvictor.join();
  105.  
  106.         // When HttpClient instance is no longer needed,
  107.         // shut down the connection manager to ensure
  108.         // immediate deallocation of all system resources
  109.         httpclient.getConnectionManager().shutdown();        
  110.     }
  111.    
  112.     public static class IdleConnectionEvictor extends Thread {
  113.        
  114.         private final ClientConnectionManager connMgr;
  115.        
  116.         private volatile boolean shutdown;
  117.        
  118.         public IdleConnectionEvictor(ClientConnectionManager connMgr) {
  119.             super();
  120.             this.connMgr = connMgr;
  121.         }
  122.  
  123.         @Override
  124.         public void run() {
  125.             try {
  126.                 while (!shutdown) {
  127.                     synchronized (this) {
  128.                         wait(5000);
  129.                         // Close expired connections
  130.                         connMgr.closeExpiredConnections();
  131.                         // Optionally, close connections
  132.                         // that have been idle longer than 5 sec
  133.                         connMgr.closeIdleConnections(5, TimeUnit.SECONDS);
  134.                     }
  135.                 }
  136.             } catch (InterruptedException ex) {
  137.                 // terminate
  138.             }
  139.         }
  140.        
  141.         public void shutdown() {
  142.             shutdown = true;
  143.             synchronized (this) {
  144.                 notifyAll();
  145.             }
  146.         }
  147.        
  148.     }
  149.    
  150. }
  151.