English 中文(简体)
"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);
  }
  params.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?

最佳回答

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?

问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签