问题描述
我有一个Java进程挂在使用以下代码的IOUtils.toString
调用中:
I have a java process that is hanging in a call to IOUtils.toString
with the following code:
String html = "";
try {
html = IOUtils.toString(someUrl.openStream(), "utf-8"); // process hangs on this line
} catch (Exception e) {
return null;
}
它不能可靠地复制此内容.它是Web爬网程序的一部分,因此成功执行了数千行,但最终导致该过程在几天后停止.
It can't reproduce this reliably. It's part of a web crawler and so executes this line thousands of times successfully but ultimately causes the process to hang here after a few days.
jstack 的输出:
2013-09-25 09:09:36
Full thread dump OpenJDK 64-Bit Server VM (20.0-b12 mixed mode):
"Attach Listener" daemon prio=10 tid=0x00007f2b1c001000 nid=0x225a waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-0" prio=10 tid=0x00007f2b34122000 nid=0x187b runnable [0x00007f2b30970000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:146)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
- locked <0x00000000e3d2d160> (a java.io.BufferedInputStream)
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:552)
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:609)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:696)
- locked <0x00000000e3d30558> (a sun.net.www.http.ChunkedInputStream)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2582)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:282)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:324)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:176)
- locked <0x00000000e3d317d0> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.Reader.read(Reader.java:140)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1364)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1340)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1315)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:525)
我看不到任何在toString方法上设置超时的方法.有什么建议?这是Apache Commons中的错误吗?还是在我的OpenJDK中?
I can't see any way to set a timeout on the toString method. Any suggestions? Is this a bug in Apache commons? Or in my OpenJDK perhaps?
推荐答案
我决定尝试使用番石榴IO来代替,因为它已经在我的类路径中了:
I've decided to try simply using guava IO instead since it was already in my classpath anyway:
String html = "";
try {
InputSupplier<? extends InputStream> supplier = Resources
.newInputStreamSupplier(metaUrl);
html = CharStreams.toString(CharStreams.newReaderSupplier(supplier,
Charsets.UTF_8));
} catch (Exception e) {
return null;
}
崩溃通常需要几天的时间,因此,如果我几天之内不更新此答案,请假设此方法有效!
It generally takes a few days to crash so if I don't update this answer in a few days, assume this worked!
更新:到目前为止7天没有挂起...:)
Update : 7 days so far without hanging... :)
这篇关于Java进程挂在IOUtils上.疑似死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!