httpClient 超時時間設置(轉)

上週樂視網cms香港接口,數據刷新緩慢。香港運營人員一直在羣裏反饋tv端內容不更新。html

我觀察了下服務器,發現請求接口耗時好久500~6000ms。以前通常都是100ms左右。java

致使不少線程一直在waiting狀態,等待網絡io。apache

因此首先找運維解決問題,原來運維人員鳥悄的給咱們中港專線限速了,😓汗一個。服務器

後來運維給恢復後系統正常。網絡

其實,看到6000ms的時候,內心咯噔一下,我記得我設置的超時時間是5000ms啊(業務須要,單條數據很大)。多線程

原來讀取超時時間沒生效,只生效了鏈接超時時間。因此網上找了資料,介紹的很詳細,試了下沒問題。app

尊重博主原創,特貼博客連接。copy下來只怕之後連接失效或刪掉。運維

轉自:http://blog.csdn.net/hi_kevin/article/details/32316171socket

HttpClient在使用中有兩個超時時間,是一直接觸和使用的,因爲上次工做中使用httpClient形成了系統悲劇的狀況,特意對它的兩個超時時間進行了小小的測試,在這裏記錄下。post

 

     測試版本爲HttpClient——3.1

 

     一:鏈接超時:connectionTimeout

 

     1:指的是鏈接一個url的鏈接等待時間。

 

     2:設置方法爲:

 

     public class TestHttpClientMain {

 /**
  * @param args
  */
 public static void main(String[] args) {
  HttpClient client = new HttpClient();

  HttpMethod method = new GetMethod(
    "http://test.com");
  client.getHttpConnectionManager().getParams()
    .setConnectionTimeout(3000);
  client.getHttpConnectionManager().getParams().setSoTimeout(3000);
  try {
   int statusCode = client.executeMethod(method);
   System.out.println(statusCode);

   byte[] responseBody = null;

   responseBody = method.getResponseBody();

   String result = new String(responseBody);

   System.out.println(result);

  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

 

 

     3:測試的時候,將url改成一個不存在的url:「http://test.com」

 

     4:超時時間3000ms事後,系統報出異常。

 

     org.apache.commons.httpclient.ConnectTimeoutException: The host did not accept the connection within timeout of 3000 ms

 at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:155)
 at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
 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)

 

 

     二:讀取數據超時:soTimeout

 

     1:指的是鏈接上一個url,獲取response的返回等待時間

 

     2:設置方法

 

     public class TestHttpClientMain {

 /**
  * @param args
  */
 public static void main(String[] args) {
  HttpClient client = new HttpClient();

  HttpMethod method = new GetMethod(
    "http://localhost:8080/firstTest.htm?method=test");
  client.getHttpConnectionManager().getParams()
    .setConnectionTimeout(3000);
  client.getHttpConnectionManager().getParams().setSoTimeout(2000);
  try {
   int statusCode = client.executeMethod(method);
   System.out.println(statusCode);

   byte[] responseBody = null;

   responseBody = method.getResponseBody();

   String result = new String(responseBody);

   System.out.println(result);

  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

 

 

     3:測試的時候的鏈接url爲我本地開啓的一個url,http://localhost:8080/firstTest.htm?method=test

 

     在我這個測試url裏,當訪問到這個連接時,線程sleep一段時間,來模擬返回response超時。

 

     @RequestMapping(params = "method=test") //<——②

    public String testMethod(ModelMap model) {
  try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
        System.out.println("call testMethod method.");
        model.addAttribute("name", "test method");
  return "test";
    }

 

 

     4:將讀取response返回超時時間設的時間比那個sleep時間短以後,運行程序給出異常:

 

      java.net.SocketTimeoutException: Read timed out

 at java.net.SocketInputStream.socketRead0(Native Method)
 at java.net.SocketInputStream.read(Unknown Source)
 at java.io.BufferedInputStream.fill(Unknown Source)
 at java.io.BufferedInputStream.read(Unknown Source)
 at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)
 at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)
 at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)
 at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)
 at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)

 

 HttpClient 4 和 HttpClient 3 設置超時

 

HttpClient 4:

鏈接超時:

鏈接超時
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000 );
// 或者
HttpConnectionParams.setConnectionTimeout(params, 6000 );

讀取超時:

 
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000 );
// 或者
HttpConnectionParams.setSoTimeout(params, 60000 );

HttpClient 3:

鏈接超時:

 
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout( 60000 );

讀取超時:

?
httpClient.getHttpConnectionManager().getParams().setSoTimeout( 60000 );

 

 

     ok,之後再寫httpClient這兩個超時時間必定要加上,不加就極可能悲劇的了

相關文章
相關標籤/搜索