如何在PoolingHttpClientConnectionManager中设置setMaxPerRoute?

时间:2016-05-31 09:17:43

标签: java connection-pooling apache-httpclient-4.x

我有$re = '(?:^|,)(?:\"(?:[^\"]+|\"\")*\"|[^,]*)' $csv = Get-Content 'your_file.csv' $count = [regex]::matches($csv[0], $re).groups.count $csv | Select -Skip 1 | % { if([regex]::matches($_, $re).groups.count -eq $count) { ...do valid stuff } else { ...do invalid stuff } } 我要为每条路线设置最大连接数。我正在以下一个方式这样做:

PoolingHttpClientConnectionManager

例如我的poolingHttpClientConnectionManager.setDefaultMaxPerRoute(5); poolingHttpClientConnectionManager.setMaxPerRoute(new HttpRoute(HttpHost.create(url)), 3); url。 因此,每个路由5和3的默认最大值为每个特定网址。然后,如果我打电话

https://repo.maven.apache.org/maven2

我收到poolingHttpClientConnectionManager.getStats(new HttpRoute(HttpHost.create(url))); PoolStats max = 3所以现在一切都还好。

但是当我使用池化连接管理器创建一个客户端并调用相同的URL时,我可以在日志中看到:

PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {s}->https://repo.maven.apache.org:443][total kept alive: 0; route allocated: 1 of 5; total allocated: 1 of 200]

我可以看到它仍然使用5个连接作为该示例url的最大值。 所以我的问题是如何设置每个路由的最大连接数以使其工作?

1 个答案:

答案 0 :(得分:0)

好的,我已设法使用下一个代码修复它:

// code to create HttpRoute the same as in apache library
private HttpRoute getHttpRouteForUrl(String url) throws URISyntaxException
{
    URI uri = new URI(url);
    boolean secure = uri.getScheme().equalsIgnoreCase("https");
    int port = uri.getPort();
    if (uri.getPort() > 0)
    {
        port = uri.getPort();
    }
    else if (uri.getScheme().equalsIgnoreCase("https"))
    {
        port = 443;
    }
    else if (uri.getScheme().equalsIgnoreCase("http"))
    {
        port = 80;
    }
    else
    {
        LOGGER.warn("Unknown port of uri %s", repository);
    }
    HttpHost httpHost = new HttpHost(uri.getHost(), port, uri.getScheme());
    // TODO check whether we need this InetAddress as second param
    return new HttpRoute(httpHost, null, secure);
}

如果我们将HttpRoute用于setMaxPerRoute,一切都会按预期运行。