我正在使用慢速Web服务(每个请求约4分钟),我需要在两小时内完成大约100个请求,所以我决定使用多个线程。问题是我只能有2个线程,因为存根拒绝所有其他线程。 Here我找到了解释和可能的解决方案:
我遇到了同样的问题。看起来 它的来源是 defaultMaxConnectionsPerHost值 MultiThreadedHttpConnectionManager 等于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并使用默认值,则只执行两个线程,如果我设置它,则应用程序不起作用。有什么想法吗?
答案 0 :(得分:1)
如果有人想在WSO2 Axis2中创建动态REST客户端,以下代码对我有用......
// Set the max connections to 20 and the timeout to 20 seconds
MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
params.setDefaultMaxConnectionsPerHost(20);
params.setMaxTotalConnections(20);
params.setSoTimeout(20000);
params.setConnectionTimeout(20000);
multiThreadedHttpConnectionManager.setParams(params);
HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
// Create the service client
ServiceClient serviceClient = new ServiceClient();
Options options = new Options();
options.setTo(new EndpointReference(endpoint));
options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
options.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_POST);
serviceClient.getServiceContext().getConfigurationContext().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
serviceClient.setOptions(options);
// Blocking call
OMElement result = serviceClient.sendReceive(ClientUtils.getRestPayload()); // just a dummy payload <root></root>
// Cleanup Transport after each call, this is needed to otherwise the HTTP gets blocked
serviceClient.cleanupTransport();
我将Max Connections设置为20,将Timeout设置为20秒。 此外,我的'端点'包含所有REST参数,我只是使用虚拟有效负载“&lt; root&gt;&lt; / root&gt;”在serviceClient.sendReceive()方法中。
答案 1 :(得分:0)
我在企业网络应用程序中注意到这一点,该应用程序调用了可能需要很长时间才能响应的后端服务。 Web应用程序将锁定,因为对单个主机的2个连接的限制将保持不变。
在致电httpConnectionManager.setParams( params )
之前,请致电params.setDefaultMaxConnectionsPerHost()
。您是否尝试以相反的顺序调用这些函数以确认params的应用不会在httpConnectionManager.setParams
函数本身内发生?