Prabhjot
Prabhjot

Reputation: 5014

HttpClient executeMethod throws java.net.SocketException: Connection reset

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.

Upvotes: 2

Views: 4630

Answers (1)

hooknc
hooknc

Reputation: 5001

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.

Upvotes: 3

Related Questions