2

I have a java program which creates multiple threads where each thread makes POST request. It works fine most of the time but under heavy load it throws Connection reset exception. For example, when I made 40 simultaneous requests, couple of time I got Connection reset exception.

Caught: java.net.SocketException: Connection reset
java.net.SocketException: Connection reset
   at java.net.SocketInputStream.read(SocketInputStream.java:168)
   at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
   at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
   at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:
   at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:105
   at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.
   at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMetho
   at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodB
   at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.j
   at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(Htt
   at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMe
   at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.jav
   at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.jav

In my Java code, I am creating an instance of HttpClient

HttpClient client = new HttpClient();

The problem does not happen when I make one request wait for it to finish and then make another request. However, the moment I turn the program into multi-threading I get this issue.

Could anyone please point out how could I prevent this exception and then retry again. From google, I found out the answer may be along the lines of using

org.apache.commons.httpclient.util.IdleConnectionTimeoutThread 

I am trying to use this in the mean time but any other suggestions are welcome.

1
  • Is there any logging server side which might give you a clue as to why this is happening? Commented Oct 20, 2014 at 20:45

1 Answer 1

3

I recommend either using a MultiThreadedHttpConnectionManager if you're using HttpClient 3 or PoolingHttpClientConnectionManager if you're using HttpClient 4.

I'm fairly confident that the default connection manager for both HttpClient 3 and HttpClient 4 are expecting only single thread access.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I changed it to use MultiThreadedHttpConnectionManager and that solved the issue of Connection reset. I want to vote for your answer but unfortunately I don't have 15 reputation.
I think you should be able to accept the answer though. Glad it worked out. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.