java httpclient ConnectTimeout

Posted in :

httpclient 有 3個 timeout 可以設定,好像大多的人是設 30 seconds. 這 3個值分別代表什麼意思?


Apache HTTP Client, you can set these timeouts using the request params:

HttpPost request = new HttpPost(url);

RequestConfig requestConfig = RequestConfig.custom()
  .setSocketTimeout(TIMEOUT_MILLIS)
  .setConnectTimeout(TIMEOUT_MILLIS)
  .setConnectionRequestTimeout(TIMEOUT_MILLIS)
  .build();

request.setConfig(requestConfig);

https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.html

getConnectionRequestTimeout()

Returns the timeout in milliseconds used when requesting a connection from the connection manager.

getConnectTimeout()

Determines the timeout in milliseconds until a connection is established.

getSocketTimeout()

Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets).

__

So, the first one, connectionRequestTimeout happens when you have a pool of connections and they are all busy, not allowing the connection manager to give you one connection to make the request.

connectTimeout happens when establishing the connection. For instance while doing the tcp handshake.

socketTimeout like the description says, is the timeout while waiting for data. Usually happens when your server is slow.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *