gpt4 book ai didi

java - 陷入SocketInputStream.socketRead0

转载 作者:行者123 更新时间:2023-12-03 11:55:36 24 4
gpt4 key购买 nike

我有一个项目,其中我在许多任务中同时下载许多页面,这些页面通过ThreadPool(大小= 200)进行处理。所有这些任务都使用相同的getPage方法下载页面(使用Apache Commons HttpClient和Apache Commons IO):

public static String getPage(String url)
throws IOException {

HttpUriRequest request = new HttpGet(url);

HttpResponse response = HTTP_CLIENT_BUILDER.build().execute(request);
try (InputStream content = response.getEntity().getContent()) {
return IOUtils.toString(content, "UTF-8");
}
}

HTTP_CLIENT_BUILDER是通过以下方式初始化的静态字段:
 private static final HttpClientBuilder HTTP_CLIENT_BUILDER = HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setSocketTimeout(SOCKET_TIMEOUT_MS) // 60_000
.setConnectTimeout(CONNECTION_TIMEOUT_MS) // 5_000
.build());

问题语句:在某个时刻(大多数任务完成时),所有其余线程都卡在 native 方法 SocketInputStream.socketRead0上,因此 jdb表示它们都在运行(嗯,是的,我希望这种行为运行 native 方法:-)):
> threads
Group system:
(java.lang.ref.Reference$ReferenceHandler)0xac4 Reference Handler cond. waiting
(java.lang.ref.Finalizer$FinalizerThread)0xac5 Finalizer cond. waiting
(java.lang.Thread)0xac6 Signal Dispatcher running
(java.lang.Thread)0xac7 Java2D Disposer cond. waiting
Group main:
(java.lang.Thread)0xac9 pool-1-thread-5 running
(java.lang.Thread)0xaca pool-1-thread-12 running
(... 12 more threads from ThreadPool ...)
(java.lang.Thread)0xad7 DestroyJavaVM running
> where 0xac9
[1] java.net.SocketInputStream.socketRead0 (native method)
[2] java.net.SocketInputStream.read (SocketInputStream.java:150)
[3] java.net.SocketInputStream.read (SocketInputStream.java:121)
[4] sun.security.ssl.InputRecord.readFully (InputRecord.java:465)
[5] sun.security.ssl.InputRecord.read (InputRecord.java:503)
[6] sun.security.ssl.SSLSocketImpl.readRecord (SSLSocketImpl.java:961)
[7] sun.security.ssl.SSLSocketImpl.performInitialHandshake (SSLSocketImpl.java:1,363)
[8] sun.security.ssl.SSLSocketImpl.startHandshake (SSLSocketImpl.java:1,391)
[9] sun.security.ssl.SSLSocketImpl.startHandshake (SSLSocketImpl.java:1,375)
[10] org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket (SSLConnectionSocketFactory.java:275)
[11] org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket (SSLConnectionSocketFactory.java:254)
[12] org.apache.http.impl.conn.HttpClientConnectionOperator.connect (HttpClientConnectionOperator.java:117)
[13] org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect (PoolingHttpClientConnectionManager.java:314)
[14] org.apache.http.impl.execchain.MainClientExec.establishRoute (MainClientExec.java:363)
[15] org.apache.http.impl.execchain.MainClientExec.execute (MainClientExec.java:219)
[16] org.apache.http.impl.execchain.ProtocolExec.execute (ProtocolExec.java:195)
[17] org.apache.http.impl.execchain.RetryExec.execute (RetryExec.java:86)
[18] org.apache.http.impl.execchain.RedirectExec.execute (RedirectExec.java:108)
[19] org.apache.http.impl.client.InternalHttpClient.doExecute (InternalHttpClient.java:186)
[20] org.apache.http.impl.client.CloseableHttpClient.execute (CloseableHttpClient.java:82)
[21] org.apache.http.impl.client.CloseableHttpClient.execute (CloseableHttpClient.java:106)
[22] <package>.Utils.getPage (Utils.java:122)
[23...] <internal details>
> # the same picture for all of them

我不明白为什么会发生这种情况,但是我发现 Java bug可能与问题有关。因此,也许我不是在寻找真正的解决方案,而是在寻找一些解决方法。

由于该错误是针对Linux提交的,我应该说,我也在使用运行Ubuntu 14.04 x86_64的虚拟机。

UPD :好的,我现在尝试的是使用 setConnectionRequestTimeout添加新的超时(只是为了确保它不起作用)和 finally添加 getPage块:
...
try (InputStream content = response.getEntity().getContent()) {
return IOUtils.toString(content, "UTF-8");
} finally {
httpClient.getConnectionManager().closeIdleConnections(0, TimeUnit.NANOSECONDS);
}

让我们看看,这是否有帮助。

UPD2 :这似乎有所帮助,但是,我的永久性运行任务每天大约卡死一次。

最佳答案

不幸的是,我没有找到任何简单的解决方法(或真正的解决方案),所以我请经理写了我自己的解决方法,希望它能对遇到此错误的人有所帮助:

创建类ConnectionSupervisor:

private static class ConnectionsSupervisor extends Thread {
private Set<RequestEntry> streams = new CopyOnWriteArraySet<>();

public ConnectionsSupervisor() {
setDaemon(true);
setName("Connections supervisor");
}

@Override
public void run() {
while (true) {
try {
Thread.sleep(CONNECTIONS_SUPERVISOR_WAIT_MS);
} catch (InterruptedException ignored) {
}
long time = timestamp();
streams.stream().filter(entry -> time > entry.timeoutBorder).forEach(entry -> {
HttpUriRequest request = entry.request;
System.err.format("HttpUriRequest killed after timeout (%d sec.) exceeded: %s%n",
FULL_CONNECTION_TIMEOUT_S,
request);
request.abort();
});
}
}

public void addRequest(HttpUriRequest request) {
streams.add(new RequestEntry(timestamp() + FULL_CONNECTION_TIMEOUT_S, request));
}

public void removeRequest(HttpUriRequest request) {
streams.removeIf(entry -> entry.request == request);
}

private static class RequestEntry {
private long timeoutBorder;
private HttpUriRequest request;

public RequestEntry(long timeoutBorder, HttpUriRequest request) {
this.timeoutBorder = timeoutBorder;
this.request = request;
}
}
}


public static long timestamp() {
return Instant.now().getEpochSecond();
}

在某个地方应该有一个 ConnectionSupervisor实例,例如:
private static final ConnectionsSupervisor connectionsSupervisor = new ConnectionsSupervisor();
static {
connectionsSupervisor.start();
}

在类似于 getPage方法中:
HttpUriRequest request = ...;

// ...

connectionsSupervisor.addRequest(request);

try (InputStream content = httpClient.execute(request).getEntity().getContent()) {
return IOUtils.toString(content, "UTF-8");
// or any other usage
} finally {
connectionsSupervisor.removeRequest(request);
// highly important!
}

关于java - 陷入SocketInputStream.socketRead0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29275043/

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