Should I still set ConnectionRequestTimeout on Apache HttpClient if I don't use a custom connection manager?

寵の児 提交于 2019-12-10 03:01:34

问题


I am using Apache RequestConfig to configure some timeouts on my HttpClient.

RequestConfig config = RequestConfig.custom()
        .setConnectTimeout(timeout)
        .setSocketTimeout(timeout)
        .setConnectionRequestTimeout(timeout) // Can I leave this out..
        .build();

CloseableHttpClient httpClient = HttpClients.custom()
        //.setConnectionManager(connectionManager) // ..if I don't use this
        .setDefaultRequestConfig(config)
        .build();

Does it make any sense to call setConnectionRequestTimeout(timeout) even I don't have a custom Connection Manager / Pool set up?

As far as I understand, setConnectionRequestTimeout(timeout) is used to set the time to wait for a connection from the connection manager/pool.

Note that I am not setting a Connection Manager on the httpClient (see commented line).


回答1:


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

So, The answer to your question of:

Does it make any sense to call setConnectionRequestTimeout(timeout) even I don't have a custom Connection Manager / Pool set up?

is YES.

This is because the default implementation has an internal connection pool. So, yes it makes sense to specify a connection request timeout. Actually it is a good, safe practice.




回答2:


Isuru's answer is mostly correct. The default connection manager is a PoolingHttpClientConnectionManager.
However, by default it will only have one connection in it's pool. If you are using your HttpClient synchronously from the same thread you should never encounter a situation where the ConnectionRequestTimeout will take effect.
If you are using the HttpClient from multiple threads then you might want to set it, but you would probably also want to increase the pool size, among other things.
For single-threaded httpclient use it is safe to leave it out.



来源:https://stackoverflow.com/questions/37857327/should-i-still-set-connectionrequesttimeout-on-apache-httpclient-if-i-dont-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!