【问题标题】:HTTPClient 4.x Connection reuse not happeningHTTPClient 4.x 连接重用没有发生
【发布时间】:2011-11-20 07:16:40
【问题描述】:

我尝试了以下 Apache http 客户端示例:

http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java

我将最大池大小设置为 5 并运行 10 个线程。运行此代码后,当我检查 netstat 时,我看到 10 个 TCP 连接正在打开。我期待连接被重用。为什么是这样 ?我错过了什么吗?

代码sn-p如下:

public class ClientMultiThreadedExecution {
public static void main(String[] args) throws Exception {

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(
    new Scheme("http", 18080, PlainSocketFactory.getSocketFactory()));

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
    cm.setDefaultMaxPerRoute(5);
    cm.setMaxTotal(5);


    HttpClient httpclient = new DefaultHttpClient(cm);
    try {
        // create an array of URIs to perform GETs on
        String uri = "http://test.webservice.com:18080/TestServlet";
        String data = "This is a test message";

        System.out.println("Started at: " + new Date());
        // creating 10 threads
        PostThread[] threads = new PostThread[10];

        for (int i = 0; i < threads.length; i++) {
        HttpPost httpPost = new HttpPost(uri);
        threads[i] = new PostThread(httpclient, httpPost, data, i + 1);
            threads[i].start();
            //Thread.sleep(1000);
        }

       // join the threads
        for (int j = 0; j < threads.length; j++) {
            threads[j].join();
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        System.out.println("Ended at: " + new Date());
        httpclient.getConnectionManager().shutdown();
    }
}

/**
 * A thread that performs a POST.
 */
static class PostThread extends Thread {

    private final HttpClient httpClient;
    private final HttpContext context;
    private final HttpPost httpPost;
    private final int id;
    private final String data;

    public PostThread(HttpClient httpClient, HttpPost httpPost, String data, int id) {
        this.httpClient = httpClient;
        this.context = new BasicHttpContext();
        this.httpPost = httpPost;
        this.id = id;
        this.data = data;
    }

    /**
     * Executes the PostMethod and prints some status information.
     */
    @Override
    public void run() {

        //System.out.println(id + " - about to get something from " + httpPost.getURI());

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("XML",data));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

            // execute the method
            HttpResponse response = httpClient.execute(httpPost, context);

            //System.out.println(id + " - get executed");
            // get the response body as an array of bytes
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                System.out.println("Success");

            //Is this step necessary ?? Need to check as only status code is required
            //httpPost.abort();
            //HttpEntity entity = response.getEntity();

            //And this ?
            //EntityUtils.consume(entity);

        } catch (Exception e) {
            httpPost.abort();
            System.out.println(id + " - error: " + e);
        }
    }

}}

【问题讨论】:

  • 解决方案是特别提到协议版本为HTTP_1_1:HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);。然后池化就起作用了。
  • 我们需要保留什么来代替params意味着我们需要在代码中放置什么值和什么地方

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


【解决方案1】:

        //Is this step necessary ?? Need to check as only status code is required
        //httpPost.abort();
        //HttpEntity entity = response.getEntity();

        //And this ?
        //EntityUtils.consume(entity);

你猜怎么着?是的。

必须确保响应内容被消耗,以便将底层连接释放回连接管理器。调用EntityUtils#consumehttpUriRequest#abort 触发释放连接回池。不同之处在于前者试图保持连接处于活动状态,而后者则不这样做。

【讨论】:

  • 这个代码块怎么样? catch (Exception e) { httpPost.abort(); System.out.println(id + " - error: " + e); } 每当发生异常时,都会调用 httpPost.abort()。但是,我观察到的是,相同的中止连接仍在分发给其他线程,并且所有线程都出错,并显示消息 'java.io.IOException: Request already aborted' 。为什么会这样?
  • @Dunxton 中止的连接会立即被丢弃并从池中逐出。您正在尝试重新使用中止的请求。不要那样做。请求对象很便宜。
  • 这个答案应该被标记为正确的答案...仍然有效,即使在 9 年后。
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多