我正在使用apache http client api 4.5来访问服务器。当服务器负载很重时,我收到错误
“[读取] I / O错误:读取超时”
这是我的配置
private CookieStore cookieStore = new BasicCookieStore();
private CloseableHttpClient httpClient;
private final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36";
private HttpClientContext httpContext = new HttpClientContext();
private RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(5000).
setConnectTimeout(10000).build();
private PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
public MyHttpClient() {
cm.setMaxTotal(50);
cm.setDefaultMaxPerRoute(10);
HttpHost host = new HttpHost("localhost", 80);
cm.setMaxPerRoute(new HttpRoute(host), 5);
DefaultServiceUnavailableRetryStrategy retryStrategy = new DefaultServiceUnavailableRetryStrategy(5, 1);
SocketConfig socketConfig = SocketConfig.custom()
.setSoKeepAlive(true)
.setSoReuseAddress(true)
.setTcpNoDelay(true)
.setSoTimeout(10000).build();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
HttpClientBuilder builder= HttpClients.custom();
builder.setMaxConnPerRoute(50)
.setConnectionManager(cm)
.setConnectionTimeToLive(10000, TimeUnit.MILLISECONDS)
.setDefaultCookieStore(cookieStore)
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultSocketConfig(socketConfig)
.setServiceUnavailableRetryStrategy(retryStrategy)
.setKeepAliveStrategy(keepAliveStrategy)
.setRetryHandler(myRetryHandler);
httpClient=builder.build();
}
我理解问题的原因
读取超时是等待读取数据的超时
但我想知道
我在这里看到了类似的帖子,但没有回答Similar issue
答案 0 :(得分:0)
我认为错误消息与其上方的响应无关。注意时间偏移。如果它是该响应的一部分,它将具有与该响应的行相同的时间戳。这就是超时发生并被记录的地方。
答案 1 :(得分:0)
出现此类错误的原因之一可能是服务器在响应后关闭HTTP连接。如果是这种情况,那么可以通过禁用客户端的连接重用来避免此错误,因此客户端不需要验证连接是否仍然存在:
builder.setConnectionReuseStrategy( (response, context) -> false );