Java HttpClient连接到主机的数量

时间:2014-06-20 13:34:11

标签: java multithreading http apache-httpclient-4.x

我有一个HttpClient向Web应用程序发起GET请求。我正在使用HttpComponents Apache Library。

我将连接管理器设置如下。

public synchronized static void registerCnalHttpClient(final Parameters params) {


         final int maxTotalConnections = 
           Integer.valueOf(params.get("max_total_connections", "100"));
        final int defaultMaxConnectionsPerHost = 
           Integer.valueOf(params.get("def_max_connections_per_host",
                "50"));
        final PoolingHttpClientConnectionManager connectionManager = 
              new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(maxTotalConnections);
        connectionManager.setDefaultMaxPerRoute(defaultMaxConnectionsPerHost);

        final HttpClientBuilder httpClientBuilder = HttpClients.custom();
        httpClientBuilder.setConnectionManager(connectionManager);
        httpClient = httpClientBuilder.build();
        esb.put(CNAL_HTTPCLIENT_STR, httpClient);

    }
}

但是,如果我从运行httpClient应用程序的linux服务器netstat,我从未看到超过13个服务器连接,我发出请求。

我在这里做错了吗?该应用程序是多线程的,这就是我选择PoolingHttpClientConnectionManager()类的原因。

为了完整起见,下面的代码显示了我如何使用httpClient发出请求

 final HttpGet httpGet = new HttpGet(uri);
        httpGet.setConfig(requestConfig);
 try {
      final CloseableHttpResponse closeableHttpResponse = cnalHttpClient.execute(httpGet);
     try {
         final HttpEntity responseEntity = closeableHttpResponse.getEntity();
         if (responseEntity != null) {
          final InputStream is = responseEntity.getContent();
          final ContentType contentType = ContentType.getOrDefault(responseEntity);
          final Charset charset = contentType.getCharset();
          response = IOUtils.toString(is, charset);

          final Map<String, String> m =
               ProjectStringUtils.splitURLQuery(response.trim());

              ...
            }catch(...)
            finally {
              try {
                closeableHttpResponse.close();
              } catch (IOException e) {
                       }
                }
           catch(...)

我希望有大约200个连接,因为那是我设置的def_max_connections_per_host。主持应用程序的人我发送请求说他们不限制我的结束允许的并发连接。

有什么想法吗?

0 个答案:

没有答案