httpclient 4.5.x多线程请求错误:尚未从此池租借



我正在为apache-httpclient的httpclient编写一个测试样本,以测试多线程请求错误。

我在循环中运行它,并在每个循环中创建2个线程,以将测试用例运行2个不同的主机设置为池管理器。

在10个循环中,我总是遇到1个错误,为:线程" thread-2" java.lang.illegalstateException:entry [id:17] [路由:{} -> http://***:8000] [state:natull]尚未从此池中租用 atorg.apache.http.util.asserts.check(asserts.java:46)

我的代码有任何问题吗?我该怎么办?

样本如下:

private void testHttpClient() {
        HttpHost proxy = new HttpHost("dev.host.com", 8001);
        final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(20);
        cm.setDefaultMaxPerRoute(1);
        HttpHost localhost1 = new HttpHost("dev.test1.com", 8001);
        cm.setMaxPerRoute(new HttpRoute(localhost1), 2);
        HttpHost localhost2 = new HttpHost("dev.test2.com", 8000);
        cm.setMaxPerRoute(new HttpRoute(localhost2), 2);
        RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(1000)
            .setSocketTimeout(1000)
            .setConnectionRequestTimeout(1000)
            .build();
        final CloseableHttpClient httpclient1 = HttpClients.custom()
            .setConnectionManager(cm)
            .setRoutePlanner(routePlanner)
            .setDefaultRequestConfig(requestConfig)
            .build();
        RequestConfig requestConfig2 = RequestConfig.custom()
            .setConnectTimeout(1000)
            .setSocketTimeout(1000)
            .setConnectionRequestTimeout(1000)
            .build();
        final CloseableHttpClient httpclient2 = HttpClients.custom()
            .setConnectionManager(cm)
            .setRoutePlanner(routePlanner)
            .setDefaultRequestConfig(requestConfig2)
            .build();
        class HttpClientThead1 implements Runnable {
          public void run() {
            for (int i = 1; i <= 1; i++) {
              System.out.println("HttpClientThead1 Start");
              HttpClientContext context = HttpClientContext.create();
              try {
                HttpGet httpget = new HttpGet("http://dev.test1.com:8001/test/id/10001");
                CloseableHttpResponse response = httpclient1.execute(httpget, context);
                long t = System.currentTimeMillis();
                System.out.println("HttpClientThead1 " + i + ":" + response.getEntity().toString() + " " + t);
                response.close();
              } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              } finally {
                cm.closeExpiredConnections();
                cm.closeIdleConnections(30, TimeUnit.SECONDS);
              }
            }
          }
        }
    class HttpClientThead2 implements Runnable {
      public void run() {
        for (int i = 1; i <= 1; i++) {
          System.out.println("HttpClientThead2 Start");
          HttpClientContext context = HttpClientContext.create();
          try {
            HttpGet httpget2 = new HttpGet("http://dev.test2.com:8000/test/id/10002");
            CloseableHttpResponse response2 = httpclient2.execute(httpget2, context);
            long t = System.currentTimeMillis();
            System.out.println("HttpClientThead2 " + i + ":" + response2.getEntity().toString() + " " + t);
            response2.close();
          } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } finally {
            cm.closeExpiredConnections();
            cm.closeIdleConnections(30, TimeUnit.SECONDS);
          }
        }
      }
    }
    for (int j = 1; j <= 10; j++) {
      HttpClientThead1 t1 = new HttpClientThead1();
      HttpClientThead2 t2 = new HttpClientThead2();
      Thread thread1 = new Thread(t1);
      Thread thread2 = new Thread(t2);
      thread1.start();
      thread2.start();
    }
}

我得到了答案,因为我在此httpclient中设置了一个代理。因此,我还需要将其设置为带有代理信息的setMaxperRoute。

所以应该是:cm.setmaxperroute(新的httproute(localhost2,proxy),10);

我相信您在httpclient中找到了一个错误;我在这里报告:https://issues.apache.org/jira/browse/httpcore-634。它仅在使用连接池时才发生,但请勿重复使用连接。

HTTP连接仅在消耗整个响应时才能重复使用。我更改了您的代码以阅读如下:

(...)
            CloseableHttpResponse response2 = httpclient2.execute(httpget2, context);
            HttpEntity entity = response.getEntity();
            // consume the response
            EntityUtils.consumeQuietly(entity);
            response2.close();
(...)

,没有失败。因此,要么消耗响应,要么不使用连接池,您就可以了。

最新更新