1. HttpClient client = new HttpClient();     
  2. HttpMethod method = new GetMethod("http://www.apache.org");     
  3. try {     
  4.   client.executeMethod(method);     
  5.   byte[] responseBody = null;     
  6.       
  7.   responseBody = method.getResponseBody();     
  8.       
  9. catch (HttpException e) {     
  10.   // TODO Auto-generated catch block     
  11.   e.printStackTrace();     
  12. catch (IOException e) {     
  13.   // TODO Auto-generated catch block     
  14.   e.printStackTrace();     
  15. }finally{     
  16.   method.releaseConnection();     
  17.       
  18. }   
  19.  
  20.  
  21.    
  22. 大部分人使用HttpClient都是使用类似上面的事例代码,包括Apache官方的例子也是如此。最近我在使用HttpClient是发现一次循环发送大量请求到服务器会导致APACHE服务器的链接被占满,后续的请求便排队等待。  
  23.  
  24. 我服务器端APACHE的配置  
  25.  
  26. Timeout 30    
  27. KeepAlive On   #表示服务器端不会主动关闭链接    
  28. MaxKeepAliveRequests 100    
  29. KeepAliveTimeout 180    
  30. 因此这样的配置就会导致每个链接至少要过180S才会被释放,这样在大量请求访问时就必然会造成链接被占满,请求等待的情况。  
  31. 在通过DEBUH后发现HttpClient在method.releaseConnection()后并没有把链接关闭,这个方法只是将链接返 回给connection manager。如果使用HttpClient client = new HttpClient()实例化一个HttpClient connection manager默认实现是使用SimpleHttpConnectionManager。SimpleHttpConnectionManager有个构 造函数如下  
  32.  /**    
  33.  * The connection manager created with this constructor will try to keep the     
  34.  * connection open (alive) between consecutive requests if the alwaysClose     
  35.  * parameter is set to <tt>false</tt>. Otherwise the connection manager will     
  36.  * always close connections upon release.    
  37.  *     
  38.  * @param alwaysClose if set <tt>true</tt>, the connection manager will always    
  39.  *    close connections upon release.    
  40.  */    
  41. public SimpleHttpConnectionManager(boolean alwaysClose) {     
  42.   super();     
  43.   this.alwaysClose = alwaysClose;     
  44. }   
  45.  
  46. 看方法注释我们就可以看到如果alwaysClose设为true在链接释放之后connection manager 就会关闭链。在我们HttpClient client = new HttpClient()这样实例化一个client时connection manager是这样被实例化的  
  47.  
  48. 1 this.httpConnectionManager = new SimpleHttpConnectionManager();   
  49.  
  50. 因此alwaysClose默认是false,connection是不会被主动关闭的,因此我们就有了一个客户端关闭链接的方法。  
  51.  
  52. 方法一:  
  53.  
  54. 把事例代码中的第一行实例化代码改为如下即可,在method.releaseConnection();之后connection manager会关闭connection 。  
  55.  
  56. 1 HttpClient client = new HttpClient(new HttpClientParams(),new SimpleHttpConnectionManager(true) );   
  57.  
  58. 方法二:  
  59.  
  60. 实例化代码使用:HttpClient client = new HttpClient();  
  61. 在method.releaseConnection();之后加上  
  62.  
  63. 1 ((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();   
  64.  
  65. shutdown源代码很简单,看了一目了然  
  66.  public void shutdown() {     
  67.   httpConnection.close();     
  68. }   
  69.  
  70. 方法三:  
  71.  
  72. 实例化代码使用:HttpClient client = new HttpClient();  
  73. 在method.releaseConnection();之后加上  
  74. client.getHttpConnectionManager().closeIdleConnections(0);此方法源码代码如下:  
  75.  public void closeIdleConnections(long idleTimeout) {     
  76.   long maxIdleTime = System.currentTimeMillis() - idleTimeout;     
  77.   if (idleStartTime <= maxIdleTime) {     
  78.     httpConnection.close();     
  79.   }     
  80. }   
  81.  
  82. 将idleTimeout设为0可以确保链接被关闭。  
  83. 以上这三种方法都是有客户端主动关闭TCP链接的方法。下面再介绍由服务器端自动关闭链接的方法。  
  84.  
  85. 方法四:  
  86.  
  87. 代码实现很简单,所有代码就和最上面的事例代码一样。只需要在HttpMethod method = new GetMethod("http://www.apache.org");加上一行HTTP头的设置即可  
  88.  
  89. 1 method.setRequestHeader("Connection""close");   
  90.  
  91. 看一下HTTP协议中关于这个属性的定义:  
  92. HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response. For example,  
  93.        Connection: close  
  94.  
  95. 现在再说一下客户端关闭链接和服务器端关闭链接的区别。如果采用客户端关闭链接的方法,在客户端的机器上使用netstat –an命令会看到很多TIME_WAIT的TCP链接。如果服务器端主动关闭链接这中情况就出现在服务器端。  
  96. 参考WIKI上的说明http://wiki.apache.org/HttpComponents/FrequentlyAskedConnectionManagementQuestions  
  97. The TIME_WAIT state is a protection mechanism in TCP. The side that closes a socket connection orderly will keep the connection in state TIME_WAIT for some time, typically between 1 and 4 minutes.  
  98. TIME_WAIT的状态会出现在主动关闭链接的这一端。TCP协议中TIME_WAIT状态主要是为了保证数据的完整传输。具体可以参考此文档:  
  99. http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html#ss2.7  
  100.  
  101. 另外强调一下使用上面这些方法关闭链接是在我们的应用中明确知道不需要重用链接时可以主动关闭链接来释放资源。如果你的应用是需要重用链接的话就没必要这么做,使用原有的链接还可以×××能。