再谈使用Commons HttpClient产生大量CLOSE_WAIT

默认情况下(即用默认构造方法建立HttpClient对象),httpMethod.releaseConnection()方法并不会关闭连接的Socket,目的是可以重用这个连接。网上有不少讨论:

这些讨论里提出了关闭Socket的方法,即在请求头里加上”Connection: close”,具体可见HTTP协议规范里的说明

其实HttpClient 3.x 里还可以通过下面的方式建立HttpClient对象,来保证关闭Socket连接:

  1. HttpClient httpClient = new HttpClient(new SimpleHttpConnectionManager(true));

具体可见HttpClient源码和API文档。

当然,使用HttpClient的多线程连接管理器时就不用这么做了。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.
For more help see http://daringfireball.net/projects/markdown/syntax

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>