如何在多线程操作中使用 HttpAsyncClient? [英] How to use HttpAsyncClient with multithreaded operation?

查看:33
本文介绍了如何在多线程操作中使用 HttpAsyncClient?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与这个问题密切相关:如何使用HttpClient进行多线程操作?,我想知道 apache HttpAsyncClient 是否是线程安全的,或者它是否也需要使用 MultiThreadedHttpConnectionManager 或 ThreadSafeClientConnManager.

Closely related to this question: How to use HttpClient with multithreaded operation?, I'm wondering if apache HttpAsyncClient is thread safe, or if it, too, requires the use of a MultiThreadedHttpConnectionManager, or a ThreadSafeClientConnManager.

如果确实需要这样的连接管理器,异步库中是否存在这样的连接管理器?

If it does require such a connection manager, does one exist in the async libraries?

我能够在异步库中找到 PoolingClientAsyncConnectionManager,但我不确定这是否是我需要的.

I was able to find a PoolingClientAsyncConnectionManager in the async libraries, but I'm not sure if that's what I need.

或者,我正在考虑使用 ThreadLocal 为每个线程创建一个 HttpAsyncClient 对象.

Alternately, I was thinking of using ThreadLocal to create one HttpAsyncClient object per thread.

请注意,与我之前引用的问题不同,我需要状态在会话之间独立,即使多个会话访问同一个域.如果在会话 1 中设置了 cookie,则该 cookie 应该对会话 2 可见.因此,我还考虑为每个请求创建一个全新的 HttpAsyncClient 对象,尽管我得到了印象中应该有更好的方法.

Note that unlike the question I referenced earlier, I need state to be independent across sessions, even if multiple sessions hit the same domain. If a cookie is set in session 1, the cookie should not be visible to session 2. For this reason, I've also considered creating a brand new HttpAsyncClient object for every single request, though I get the impression there should be a better way.

谢谢.

推荐答案

在使用和不使用 PoolingClientAsyncConnectionManager 进行负载测试后,我们发现不使用 PoolingClientAsyncConnectionManager 时得到的结果不一致.

After load testing both with and without the PoolingClientAsyncConnectionManager, we discovered that we got inconsistent results when we did not use the PoolingClientAsyncConnectionManager.

除其他外,我们跟踪了我们进行的 Http 调用的数量,以及已完成的 Http 调用的数量(通过 cancelled(...)、completed(...) 或 failed(...) 相关 FutureCallback 的函数).在没有 PoolingClientAsyncConnectionManager 的情况下,在重负载的情况下,这两个数字有时不匹配,让我们相信在某个地方,某些连接正在踩踏其他线程的连接信息(只是猜测).

Amongst other things, we tracked the number of Http calls we were making, and the number of Http calls that were completed (either through the cancelled(...), completed(...), or failed(...) functions of the associated FutureCallback). Without the PoolingClientAsyncConnectionManager, and under heavy load, the two figures sometimes did not match up, leading us to believe that somewhere, some connections were stomping on connection information from other threads (just a guess).

无论哪种方式,使用 PoolingClientAsyncConnectionManager 时,数字始终匹配,并且负载测试均成功,因此我们肯定会使用它.

Either way, with the PoolingClientAsyncConnectionManager, the figures always matched, and the load tests were all successful, so we are definitely using it.

我们使用的最终代码是这样的:

The final code we used goes like this:

public class RequestProcessor {
  private RequestProcessor instance = new RequestProcessor();
  private PoolingClientAsyncConnectionManager pcm = null;
  private HttpAsyncClient httpAsyncClient = null;
  private RequestProcessor() {
    // Initialize the PoolingClientAsyncConnectionManager, and the HttpAsyncClient 
  }
  public void process(...) {
    this.httpAsyncClient.execute(httpMethod, 
         new BasicHttpContext(), // Use a separate HttpContext for each request so information is not shared between requests
         new FutureCallback<HttpResponse>() {
      @Override
      public void cancelled() {
        // Do stuff
      }
      @Override
      public void completed(HttpResponse httpResponse) {
        // Do stuff
      }
      @Override
      public void failed(Exception e) {
        // Do stuff
      }
    });
  }
}

这篇关于如何在多线程操作中使用 HttpAsyncClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆