[java] view plaincopy
- java.net.SocketException: Too many open files
- at java.net.Socket.createImpl(Socket.java:397)
- at java.net.Socket.<init>(Socket.java:371)
- at java.net.Socket.<init>(Socket.java:249)
- at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
- at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
- at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
- at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
- at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
- at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
- at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
- at com.thhc.mylegist.baidu.BaiDuHttpFactory.getQuestionList(BaiDuHttpFactory.java:284)
- at com.thhc.mylegist.baidu.BaiDuTask.run(BaiDuTask.java:21)
- at java.util.TimerThread.mainLoop(Timer.java:512)
- at java.util.TimerThread.run(Timer.java:462)
下面是轉載的内容:
[java] view plaincopy
- Java代碼
- 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.}
- HttpClient client = new HttpClient();
- HttpMethod method = new GetMethod("http://www.apache.org");
- try {
- client.executeMethod(method);
- byte[] responseBody = null;
- responseBody = method.getResponseBody();
- } catch (HttpException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- method.releaseConnection();
- }
- 大部分人使用HttpClient都是使用類似上面的事例代碼,包括Apache官方的例子也是如此。最近我在使用HttpClient是發現一次循環發送大量請求到伺服器會導緻APACHE伺服器的連結被占滿,後續的請求便排隊等待。
- 我伺服器端APACHE的配置
- Java代碼
- 1.Timeout 30
- 2.KeepAlive On #表示伺服器端不會主動關閉連結
- 3.MaxKeepAliveRequests 100
- 4.KeepAliveTimeout 180
- Timeout 30
- KeepAlive On #表示伺服器端不會主動關閉連結
- MaxKeepAliveRequests 100
- KeepAliveTimeout 180
- 是以這樣的配置就會導緻每個連結至少要過180S才會被釋放,這樣在大量請求通路時就必然會造成連結被占滿,請求等待的情況。
- 在通過DEBUH後發現HttpClient在method.releaseConnection()後并沒有把連結關閉,這個方法隻是将連結傳回給connection manager。如果使用HttpClient client = new HttpClient()執行個體化一個HttpClient connection manager預設實作是使用SimpleHttpConnectionManager。SimpleHttpConnectionManager有個構造函數如下
- Java代碼
- 1.
- 10.public SimpleHttpConnectionManager(boolean alwaysClose) {
- 11. super();
- 12. this.alwaysClose = alwaysClose;
- 13.}
- public SimpleHttpConnectionManager(boolean alwaysClose) {
- super();
- this.alwaysClose = alwaysClose;
- }
- 看方法注釋我們就可以看到如果alwaysClose設為true在連結釋放之後connection manager 就會關閉鍊。在我們HttpClient client = new HttpClient()這樣執行個體化一個client時connection manager是這樣被執行個體化的
- Java代碼
- 1.this.httpConnectionManager = new SimpleHttpConnectionManager();
- this.httpConnectionManager = new SimpleHttpConnectionManager();
- 是以alwaysClose預設是false,connection是不會被主動關閉的,是以我們就有了一個用戶端關閉連結的方法。
- 方法一:
- 把事例代碼中的第一行執行個體化代碼改為如下即可,在method.releaseConnection();之後connection manager會關閉connection 。
- Java代碼
- 1.HttpClient client = new HttpClient(new HttpClientParams(),new SimpleHttpConnectionManager(true) );
- HttpClient client = new HttpClient(new HttpClientParams(),new SimpleHttpConnectionManager(true) );
- 方法二:
- 執行個體化代碼使用:HttpClient client = new HttpClient();
- 在method.releaseConnection();之後加上
- Java代碼
- 1.((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();
- ((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();
- shutdown源代碼很簡單,看了一目了然
- Java代碼
- 1.public void shutdown() {
- 2. httpConnection.close();
- 3.}
- public void shutdown() {
- httpConnection.close();
- }
- 方法三:
- 執行個體化代碼使用:HttpClient client = new HttpClient();
- 在method.releaseConnection();之後加上
- client.getHttpConnectionManager().closeIdleConnections(0);此方法源碼代碼如下:
- Java代碼
- 1.public void closeIdleConnections(long idleTimeout) {
- 2. long maxIdleTime = System.currentTimeMillis() - idleTimeout;
- 3. if (idleStartTime <= maxIdleTime) {
- 4. httpConnection.close();
- 5. }
- 6.}
- public void closeIdleConnections(long idleTimeout) {
- long maxIdleTime = System.currentTimeMillis() - idleTimeout;
- if (idleStartTime <= maxIdleTime) {
- httpConnection.close();
- }
- }
- 将idleTimeout設為0可以確定連結被關閉。
- 以上這三種方法都是有用戶端主動關閉TCP連結的方法。下面再介紹由伺服器端自動關閉連結的方法。
- 方法四:
- 代碼實作很簡單,所有代碼就和最上面的事例代碼一樣。隻需要在HttpMethod method = new GetMethod("http://www.apache.org");加上一行HTTP頭的設定即可
- Java代碼
- 1.method.setRequestHeader("Connection", "close");
- method.setRequestHeader("Connection", "close");
- 看一下HTTP協定中關于這個屬性的定義:
- 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,
- Connection: close
- 現在再說一下用戶端關閉連結和伺服器端關閉連結的差別。如果采用用戶端關閉連結的方法,在用戶端的機器上使用netstat –an指令會看到很多TIME_WAIT的TCP連結。如果伺服器端主動關閉連結這中情況就出現在伺服器端。
- 參考WIKI上的說明http://wiki.apache.org/HttpComponents/FrequentlyAskedConnectionManagementQuestions
- 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.
- TIME_WAIT的狀态會出現在主動關閉連結的這一端。TCP協定中TIME_WAIT狀态主要是為了保證資料的完整傳輸。具體可以參考此文檔:
- http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html#ss2.7
- 另外強調一下使用上面這些方法關閉連結是在我們的應用中明确知道不需要重用連結時可以主動關閉連結來釋放資源。如果你的應用是需要重用連結的話就沒必要這麼做,使用原有的連結還可以提供性能。
評論:
[java] view plaincopy
- 8 樓 javatar 2008-11-23 引用
- 官方文檔有建議使用空閑連接配接檢查線程:
- Java代碼
- 1.import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
- 2.// 建立線程
- 3.IdleConnectionTimeoutThread thread = new IdleConnectionTimeoutThread();
- 4.// 注冊連接配接管理器
- 5.thread.addConnectionManager(httpClient.getHttpConnectionManager());
- 6.// 啟動線程
- 7.thread.start();
- 8.// 在最後,關閉線程
- 9.thread.shutdown();
- import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread;
- // 建立線程
- IdleConnectionTimeoutThread thread = new IdleConnectionTimeoutThread();
- // 注冊連接配接管理器
- thread.addConnectionManager(httpClient.getHttpConnectionManager());
- // 啟動線程
- thread.start();
- // 在最後,關閉線程
- thread.shutdown();
- 7 樓 sdh5724 2008-10-29 引用
- 樓上的說法是對的, 但是, 性能來說, 是不合适的.
- 首先, 我想問你, 既然是輪詢, 為什麼HttpURLConnection要每次建立. 既然是個持續的過程, 就不應該每次HttpURLConnection對象重新建立, 你應該重複使用這個對象. HttpURLConnection會持有連接配接的, 如果斷開, 我記得他會去嘗試連接配接.
- 6 樓 SeanHe 2008-10-28 引用
- kjah 寫道
- 但使用netstat發現很多TIME_WAIT,時間長點後會出現端口都被占用的狀況 address already in use :connect,使用HttpURLConnection.disconnect()也沒有效果。
- 如果你是應為用戶端出現很多的TIME_WAIT造成端口占用,你不妨試一下“方法四”在HTTP請求頭上設定"Connection", "close"這樣伺服器會來主動關閉連結,這樣就不會在用戶端産生很多的TIME_WAIT
- 5 樓 kjah 2008-10-28 引用
- 我是google到這裡的。。。
- 您對HttpClient 的關閉分析的很透徹
- 請教HttpURLConnection和URLConnection怎麼關閉連結?
- 我需要輪詢一個網址,但使用netstat發現很多TIME_WAIT,時間長點後會出現端口都被占用的狀況 address already in use :connect,使用HttpURLConnection.disconnect()也沒有效果。
- 4 樓 fly_ever 2008-09-11 引用
- 引用
- 另外強調一下使用上面這些方法關閉連結是在我們的應用中明确知道不需要重用連結時可以主動關閉連結來釋放資源。如果你的應用是需要重用連結的話就沒必要這麼做,使用原有的連結還可以提供性能。
- 正如你所說,實際上是隻有并發量很高的時候才會發生連結占滿的情況。
- 而如果沒有這麼高的并發量,我們沒必要去實作代碼來關閉連接配接。
- 因為我們在代碼中實作主動關閉的功能的同時,也喪失了性能上的優勢。
- 是以,我覺得遇到這種情況,應該把設定中的keepAliveTimeout調低,顯得更好一些。
- Java代碼
- 1.1. Timeout 30
- 2.2. KeepAlive On #表示伺服器端不會主動關閉連結
- 3.3. MaxKeepAliveRequests 100
- 4.4. KeepAliveTimeout 180
- 1. Timeout 30
- 2. KeepAlive On #表示伺服器端不會主動關閉連結
- 3. MaxKeepAliveRequests 100
- 4. KeepAliveTimeout 180
- 不過,這裡提供的HttpClient關閉的詳細資訊,還是很有價值的。
- 3 樓 jorsef 2008-09-10 引用
- 受用,非常感謝
- 2 樓 SeanHe 2008-09-06 引用
- HttpClient client = new HttpClient(); 如果這樣進行執行個體化,預設使用SimpleHttpConnectionManager作為connection manager,SimpleHttpConnectionManager沒有連接配接池,隻管理一個連接配接
- 1 樓 JavaTestJava 2008-09-05 引用
- 方法二:
- 執行個體化代碼使用:HttpClient client = new HttpClient();
- 在method.releaseConnection();之後加上
- Java代碼
- ((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();
- ((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();
- shutdown源代碼很簡單,看了一目了然
- Java代碼
- public void shutdown() {
- httpConnection.close();
- }
- public void shutdown() {
- httpConnection.close();
- }
- 方法二中的httpConnection.close();這裡沒有參數麼?
- httpConnection為預設全局的那個連接配接?