本文介绍了如何在多线程操作中使用HttpAsyncClient?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果确实需要这样的连接管理器,那么异步库中是否存在这样的连接管理器?
If it does require such a connection manager, does one exist in the async libraries?
我能够在异步库中找到PoolingClientAsyncConnectionManager,但是我不确定这是否是我所需要的.
或者,我正在考虑使用ThreadLocal在每个线程中创建一个HttpAsyncClient对象.
Alternately, I was thinking of using ThreadLocal to create one HttpAsyncClient object per thread.
推荐答案
无论哪种方式,使用PoolingClientAsyncConnectionManager时,数字始终匹配,并且负载测试都成功完成,因此我们肯定会使用它.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!