Today Christian and I fought hard with a misterious connection problem. As a part of a longer process a web service call was failing from time to time. Actually it failed only under load. Due to that we did just debugging outputs, rather than breakpoints, as we were afraid of breaking some runtime behaviour by stopping the threads. But that was not so trivial as we would liked it to have: The webservice client is based upon the Apache Commons HTTPclient . The webserive itself was realized in Struts2 using its Restful2ActionMappers . Both got several services injected via Spring.
Initially we were after the unusual large size of the request and response, but after some analysis it seemed ok, however we still got flooded by variations of:
1java.io.IOException: CRLF expected at end of chunk: 72/84 2java.io.IOException: Bad chunk size: somexml
As we realized that this has to happen on the HTTP transport level, we had a look around and found following spring config for our bean:
1<bean name="restClient" class="de.codecentric.framework.RestClient"> 2 <property name="httpClient"> 3 <bean class="org.apache.commons.httpclient.HttpClient" /> 4 </property> 5</bean>
Doesn’t look bad, but by default the HttpClient has a ConnectionManager which will return always the same Connection instance. Even to different threads. Once you get to know this you will find immediately
which unfortunately did not help much. In addition we now go an
1java.io.IOException: connection closed
What might suprise you is that MultiThreadedHttpConnectionManager is multithreading capable, but does only allow 2 connections per host. After we did increase that out application was running fine again. Even under load.
The Spring config does look similar to this now:
1<bean name="restClient" class="de.codecentric.framework.RestClient"> 2 <property name="httpClient"> 3 <bean class="org.apache.commons.httpclient.HttpClient"> 4 <property name="httpConnectionManager"> 5 <bean class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager" destroy-method="shutdown"> 6 <property name="params"> 7 <bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams"> 8 <property name="defaultMaxConnectionsPerHost" value="20" /> 9 </bean> 10 </property> 11 </bean> 12 </property> 13 </bean> 14 </property> 15</bean>
Leasson learnt: When multiple threads are using the same UrlConnection, it can go fine, but more likely, some very obscure things (exceptions) can (will) happen 🙂
Your job at codecentric?
More articles in this subject area
Discover exciting further topics and let the codecentric world inspire you.