HttpClient executeMethod抛出java.net.SocketException:连接重置

时间:2014-10-20 20:42:27

标签: java multithreading apache-commons-httpclient

我有一个java程序,它创建多个线程,每个线程发出POST请求。它在大多数情况下工作正常,但在重负载下会抛出连接重置异常。例如,当我同时发出40个请求时,我有几次连接重置异常。

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

在我的Java代码中,我正在创建一个HttpClient实例

HttpClient client = new HttpClient();

当我发出一个请求等待它完成然后再发出另一个请求时,问题就不会发生。但是,当我将程序转换为多线程时,我遇到了这个问题。

有人可以指出我如何防止此异常然后再次重试。从谷歌,我发现答案可能是使用

org.apache.commons.httpclient.util.IdleConnectionTimeoutThread 

我正在尝试同时使用它,但欢迎任何其他建议。

1 个答案:

答案 0 :(得分:2)

如果您正在使用HttpClient 3,我建议您使用MultiThreadedHttpConnectionManager;如果您使用的是HttpClient 4,我建议使用PoolingHttpClientConnectionManager

我非常确信HttpClient 3和HttpClient 4的默认连接管理器只需要单线程访问。