本文介绍了HttpClient 中的 setDefaultMaxPerRoute 和 setMaxTotal 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个项目中使用 Apache HttpClient.我也在使用 PoolingHttpClientConnectionManager 和我的 HttpClient.

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

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

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
  • 设置连接超时
  • setSocketTimeout
  • setConnectionRequestTimeout
  • 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?

我使用的是 http-client 4.3.1

I am using http-client 4.3.1

推荐答案

http://hc.apache.org/httpclient-3.x/preference-api.html

其他必须从源头收集.

  • setMaxTotal

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

The maximum number of connections allowed across all routes.

  • setDefaultMaxPerRoute

未通过调用 setMaxPerRoute 指定的路由所允许的最大连接数.提前知道路由时使用 setMaxPerRoute,不知道时使用 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.

  • setConnectTimeout

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

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

  • setSocketTimeout

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

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_TIMEOUT for details.

  • 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

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

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

这篇关于HttpClient 中的 setDefaultMaxPerRoute 和 setMaxTotal 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:38