我正在使用一个缓慢的网络服务(每个请求大约 4 分钟),我需要在两个小时内完成大约 100 个请求,所以我决定使用多个线程。问题是我只能有 2 个线程,因为存根拒绝所有其他线程。在这里,我找到了解释和可能的解决方案:
我有同样的问题。它的来源似乎是 MultiThreadedHttpConnectionManager 中的 defaultMaxConnectionsPerHost 值等于 2。我的解决方法是创建自己的 MultiThreadedHttpConnectionManager 实例并在服务存根中使用它,如下例所示
我已经按照作者所说的做了,并将 HttpClient 传递给具有更高setMaxTotalConnections和setDefaultMaxConnectionsPerHost值的存根,但问题是现在应用程序冻结了(好吧,它并没有真正冻结,但它什么也不做)。
那是我的代码:
public ReportsStub createReportsStub(String url, HttpTransportProperties.Authenticator auth){
ReportsStub stub = null;
HttpClient httpClient = null;
try {
stub = new ReportsStub(url);
httpClient = createHttpClient(10,5);
stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(10000000);
stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth);
stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, false);
stub._getServiceClient().getServiceContext().getConfigurationContext().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
return stub;
} catch (AxisFault e) {
e.printStackTrace();
}
return stub;
}
protected HttpClient createHttpClient(int maxTotal, int maxPerHost) {
MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = httpConnectionManager.getParams();
if (params == null) {
params = new HttpConnectionManagerParams();
httpConnectionManager.setParams(params);
}
params.setMaxTotalConnections(maxTotal);
params.setDefaultMaxConnectionsPerHost(maxPerHost);
HttpClient httpClient = new HttpClient(httpConnectionManager);
return httpClient;
}
然后我将该存根和请求传递给每个线程并运行它们。如果我不设置 HttpClient 并使用默认值,则只有两个线程执行,如果我设置它,应用程序将无法运行。任何想法?