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

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

问题描述

与此问题密切相关:如何将HttpClient与多线程操作一起使用? /a>,我想知道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天全站免登陆