gpt4 book ai didi

java - HttpClient executeMethod 抛出 java.net.SocketException : Connection reset

转载 作者:行者123 更新时间:2023-11-30 09:01:11 24 4
gpt4 key购买 nike

我有一个 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 

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

最佳答案

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

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

关于java - HttpClient executeMethod 抛出 java.net.SocketException : Connection reset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com