java HttpClient 中的 setDefaultMaxPerRoute 和 setMaxTotal 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30689995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:30:33  来源:igfitidea点击:

What does setDefaultMaxPerRoute and setMaxTotal mean in HttpClient?

javaapache-httpclient-4.x

提问by john

I am using Apache HttpClient in one of my project. I am also using PoolingHttpClientConnectionManageralong with my HttpClient as well.

我在我的一个项目中使用 Apache HttpClient。我也PoolingHttpClientConnectionManager与我的 HttpClient 一起使用。

I am confuse what are these properties mean. I tried going through documentation in the code but I don't see any documentation around these variables so was not able to understand.

我很困惑这些属性是什么意思。我尝试查看代码中的文档,但没有看到有关这些变量的任何文档,因此无法理解。

  • setMaxTotal
  • setDefaultMaxPerRoute
  • setConnectTimeout
  • setSocketTimeout
  • setConnectionRequestTimeout
  • setStaleConnectionCheckEnabled
  • 设置最大总计
  • setDefaultMaxPerRoute
  • 设置连接超时
  • 设置套接字超时
  • 设置连接请求超时
  • setStaleConnectionCheckEnabled

Below is how I am using in my code:

以下是我在代码中的使用方式:

RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000).setSocketTimeout(5 * 1000)
        .setStaleConnectionCheckEnabled(false).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(200);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(20);

CloseableHttpClient httpClientBuilder = HttpClientBuilder.create()
        .setConnectionManager(poolingHttpClientConnectionManager).setDefaultRequestConfig(requestConfig)
        .build();

Can anyone explain me these properties so that I can understand and decide what values I should put in there. Also, are there any other properties that I should use apart from as shown above to get better performance?

任何人都可以向我解释这些属性,以便我可以理解并决定我应该在其中放入哪些值。另外,除了上面显示的之外,我还应该使用其他任何属性来获得更好的性能吗?

I am using http-client 4.3.1

我正在使用 http 客户端 4.3.1

采纳答案by Pace

Some parameters are explained at http://hc.apache.org/httpclient-3.x/preference-api.html

http://hc.apache.org/httpclient-3.x/preference-api.html解释了一些参数

Others must be gleaned from the source.

其他的必须从源头收集。

  • setMaxTotal
  • 设置最大总计

The maximum number of connections allowed across all routes.

所有路由允许的最大连接数。

  • setDefaultMaxPerRoute
  • setDefaultMaxPerRoute

The maximum number of connections allowed for a route that has not been specified otherwise by a call to setMaxPerRoute. Use setMaxPerRoute when you know the route ahead of time and setDefaultMaxPerRoute when you do not.

未通过调用 setMaxPerRoute 指定的路由所允许的最大连接数。当您提前知道路由时使用 setMaxPerRoute,当您不知道时使用 setDefaultMaxPerRoute。

  • setConnectTimeout
  • 设置连接超时

How long to wait for a connection to be established with the remote server before throwing a timeout exception.

在抛出超时异常之前等待与远程服务器建立连接的时间。

  • setSocketTimeout
  • 设置套接字超时

How long to wait for the server to respond to various calls before throwing a timeout exception. See http://docs.oracle.com/javase/1.5.0/docs/api/java/net/SocketOptions.html#SO_TIMEOUTfor details.

在抛出超时异常之前等待服务器响应各种调用的时间。有关详细信息,请参阅http://docs.oracle.com/javase/1.5.0/docs/api/java/net/SocketOptions.html#SO_TIMEOUT

  • setConnectionRequestTimeout
  • 设置连接请求超时

How long to wait when trying to checkout a connection from the connection pool before throwing an exception (the connection pool won't return immediately if, for example, all the connections are checked out).

在抛出异常之前尝试从连接池签出连接时等待多长时间(例如,如果所有连接都被签出,则连接池不会立即返回)。

  • setStaleConnectionCheckEnabled
  • setStaleConnectionCheckEnabled

Can be disabled for a slight performance improvement at the cost of potential IOExceptions. See http://hc.apache.org/httpclient-3.x/performance.html#Stale_connection_check

可以以潜在的 IOExceptions 为代价禁用以稍微提高性能。见http://hc.apache.org/httpclient-3.x/performance.html#Stale_connection_check