HttpClient 中的 setMaxTotal 和 setDefaultMaxPerRoute?
setMaxTotal and setDefaultMaxPerRoute in HttpClient?
我正在使用 RestTemplate
和 HttpComponentsClientHttpRequestFactory
,如下所示:
private RestTemplate restTemplate = new RestTemplate();
// singleton class so only one instance
public DataProcess() {
restTemplate.setRequestFactory(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(1000)
.setSocketTimeout(1000).setStaleConnectionCheckEnabled(false).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(300);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(300);
CloseableHttpClient httpClientBuilder = HttpClientBuilder.create()
.setConnectionManager(poolingHttpClientConnectionManager).setMaxConnPerRoute(300).setMaxConnTotal(300)
.setDefaultRequestConfig(requestConfig).build();
requestFactory.setHttpClient(httpClientBuilder);
return requestFactory;
}
现在我的问题是:在PoolingHttpClientConnectionManager
对象中设置setMaxTotal
和setDefaultMaxPerRoute
与设置setMaxConnPerRoute
有什么区别和 CloseableHttpClient
中的 setMaxConnTotal
。我需要在这两个地方设置这些吗?
还有我们应该与 RestTemplate 一起使用的任何其他设置吗?我通过使用 RestTemplate 作为我的 HttpClient 来调用 Restful 服务,所以它必须很快,因为这段代码在多线程应用程序中将是 运行。
构建器参数用于构建默认的池连接管理器(如果您不提供的话)。因此,在您的示例中,构建器上的集合是多余的。
请参阅 HttpClientBuilder(抱歉,我不得不将这个巨大的方法剪掉很多):
public CloseableHttpClient build() {
... snip ...
HttpClientConnectionManager connManagerCopy = this.connManager;
if (connManagerCopy == null) {
... snip ...
@SuppressWarnings("resource")
final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactoryCopy)
.build(),
null,
null,
null,
connTimeToLive,
connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS);
... snip ...
if (maxConnTotal > 0) {
poolingmgr.setMaxTotal(maxConnTotal);
}
if (maxConnPerRoute > 0) {
poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
}
connManagerCopy = poolingmgr;
}
... snip ...
return new InternalHttpClient(... snip ...);
}
我正在使用 RestTemplate
和 HttpComponentsClientHttpRequestFactory
,如下所示:
private RestTemplate restTemplate = new RestTemplate();
// singleton class so only one instance
public DataProcess() {
restTemplate.setRequestFactory(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(1000)
.setSocketTimeout(1000).setStaleConnectionCheckEnabled(false).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(300);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(300);
CloseableHttpClient httpClientBuilder = HttpClientBuilder.create()
.setConnectionManager(poolingHttpClientConnectionManager).setMaxConnPerRoute(300).setMaxConnTotal(300)
.setDefaultRequestConfig(requestConfig).build();
requestFactory.setHttpClient(httpClientBuilder);
return requestFactory;
}
现在我的问题是:在PoolingHttpClientConnectionManager
对象中设置setMaxTotal
和setDefaultMaxPerRoute
与设置setMaxConnPerRoute
有什么区别和 CloseableHttpClient
中的 setMaxConnTotal
。我需要在这两个地方设置这些吗?
还有我们应该与 RestTemplate 一起使用的任何其他设置吗?我通过使用 RestTemplate 作为我的 HttpClient 来调用 Restful 服务,所以它必须很快,因为这段代码在多线程应用程序中将是 运行。
构建器参数用于构建默认的池连接管理器(如果您不提供的话)。因此,在您的示例中,构建器上的集合是多余的。
请参阅 HttpClientBuilder(抱歉,我不得不将这个巨大的方法剪掉很多):
public CloseableHttpClient build() {
... snip ...
HttpClientConnectionManager connManagerCopy = this.connManager;
if (connManagerCopy == null) {
... snip ...
@SuppressWarnings("resource")
final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactoryCopy)
.build(),
null,
null,
null,
connTimeToLive,
connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS);
... snip ...
if (maxConnTotal > 0) {
poolingmgr.setMaxTotal(maxConnTotal);
}
if (maxConnPerRoute > 0) {
poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
}
connManagerCopy = poolingmgr;
}
... snip ...
return new InternalHttpClient(... snip ...);
}