0

我正在使用一个缓慢的网络服务(每个请求大约 4 分钟),我需要在两个小时内完成大约 100 个请求,所以我决定使用多个线程。问题是我只能有 2 个线程,因为存根拒绝所有其他线程。在这里,我找到了解释和可能的解决方案:

我有同样的问题。它的来源似乎是 MultiThreadedHttpConnectionManager 中的 defaultMaxConnectionsPerHost 值等于 2。我的解决方法是创建自己的 MultiThreadedHttpConnectionManager 实例并在服务存根中使用它,如下例所示

我已经按照作者所说的做了,并将 HttpClient 传递给具有更高setMaxTotalConnectionssetDefaultMaxConnectionsPerHost值的存根,但问题是现在应用程序冻结了(好吧,它并没有真正冻结,但它什么也不做)。

那是我的代码:

 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 并使用默认值,则只有两个线程执行,如果我设置它,应用程序将无法运行。任何想法?

4

2 回答 2

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 参数,我只是在 serviceClient.sendReceive() 方法中使用了一个虚拟有效负载“<root></root>”。

于 2011-11-07T10:43:49.497 回答
0

我在一个调用后端服务的公司 Web 应用程序中注意到了这一点,该服务可能需要很长时间才能响应。Web 应用程序将被锁定,因为到单个主机的 2 个连接的限制将占据上风。

你打电话httpConnectionManager.setParams( params ) 之前打电话params.setDefaultMaxConnectionsPerHost()。您是否尝试过以相反的顺序调用这些函数以确认参数的应用不会发生在httpConnectionManager.setParams函数本身内?

于 2009-11-24T17:19:37.410 回答