Wednesday, September 29, 2010

Axis2 1.5 ConnectionPoolTimeoutException & Solution

We use axis2 1.5.1 for webservices. We used to get the below exception when multiple users are connected to the application simultaneously. Once you get this exception the webservice becomes unresponsive and the application goes down.

Caused by: org.apache.commons.httpclient.ConnectionPoolTimeoutException: Timeout waiting for connection
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.doGetConnection(MultiThreadedHttpConnectionManager.java:497)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.getConnectionWithTimeout(MultiThreadedHttpConnectionManager.java:416)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:153)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:346)
at org.apache.axis2.transport.http.AbstractHTTPSender.executeMethod(AbstractHTTPSender.java:542)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:199)

The reason for this exception is that Axis2 keeps an Http Connection Pool in the configuration context which is used for making webservice calls. This pool is maintained by axis and its default size is two. If more than two webservice calls are made at the same time, then axis2 will throw this exception after waiting for some time.

This exception is likely to happen in any application that uses Axis2 1.5.1 when concurrent users are accessing the system.

Solution for this is to set the context properties REUSE_HTTP_CLIENT and AUTO_RELEASE_CONNECTION to true for releasing and reusing the used connections. And set the HttpClient as shown below.

import org.apache.axis2.client.Stub;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

private void setContextProperties(Stub stub) {
ConfigurationContext context = stub._getServiceClient().getServiceContext().getConfigurationContext();
context.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, true);
context.setProperty(HTTPConstants.AUTO_RELEASE_CONNECTION, true);
MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
params.setDefaultMaxConnectionsPerHost(20); //Set this value as per your need.
multiThreadedHttpConnectionManager.setParams(params);
HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
context.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
}

You may refer the link http://osdir.com/ml/axis-user-ws.apache.org/2010-01/msg00135.html for more details.

Labels: , ,


Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]