开发者

"Time out" when multithreading requests to a webservice with java and axis2

I'm working with a slow webservice (about 4 minutes each request) and I need to do about 100 requests in two hours, so I've decided to use multiple threads. The problem is that I can only have 2 threads, as the stub rejects all the other ones. Here I've found an explanation and possible solution:

I had the same problem. It seems that the source of it is defaultMaxConnectionsPerHost value in MultiThreadedHttpConnectionManager equals 2. Workaround for me was to create own instance of MultiThreadedHttpConnectionManager and use it in service stub, something like in example below

I've done as the author said, and passed a HttpClient to the stub with higher setMaxTotalConnections and setDefaultMaxConnectionsPerHost values, but the problem is that now the application freezes (well, it does not really freezes, but It does nothing).

Thats my code:

 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);
  }
  para开发者_如何学运维ms.setMaxTotalConnections(maxTotal);
  params.setDefaultMaxConnectionsPerHost(maxPerHost);
  HttpClient httpClient = new HttpClient(httpConnectionManager);
  return httpClient;
}

Then I pass that stub and the request to each one of threads and run them. If I don't set the HttpClient and use the default, only two threads execute, and if I set it, the application does not work. Any idea?


If anyone wants to create a dynamic REST client in WSO2 Axis2, the following code worked for me...

// 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();

I put the Max Connections to 20 and the Timeout to 20 seconds. Also my 'endpoint' contains all the REST arguments, I'm just using a dummy payload "<root></root>" in the serviceClient.sendReceive() method.


I noticed this in a corporate web application that called a back-end service that could take a long period to respond. The web application would lock up because a limit of 2 connections to a single host would take hold.

You call httpConnectionManager.setParams( params ) before you call params.setDefaultMaxConnectionsPerHost(). Have you tried calling these functions in the opposite order to confirm that application of params doesn't take place within the httpConnectionManager.setParams function itself?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜