Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto make a function "timeoutable" in Java?

Tags:

java

timeout

private String indexPage(URL currentPage) throws IOException {
    String content = "";
    is = currentPage.openStream();
    content = new Scanner( is ).useDelimiter( "\\Z" ).next();
    return content;
}

This is my function with which I'm currently crawling webpages. The function that a problem is:

content = new Scanner( is ).useDelimiter( "\\Z" ).next();

If the webpage doesn't answer or takes a long time to answer, my thread just hangs at the above line. What's the easiest way to abort this function, if it takes longer than 5 seconds to load fully load that stream?

Thanks in advance!

like image 571
ndee Avatar asked Jul 14 '26 16:07

ndee


2 Answers

Instead of struggling with a separate watcher thread, it might be enough for you (although not exactly an answer to your requirement) if you enable connect and read timeouts on the network connection, e.g.:

URL url = new URL("...");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
InputStream is = conn.getInputStream();

This example will fail if it takes more than 5 seconds (5000ms) to connect to the server or if you have to wait more than 10 seconds (10000ms) between any content chunks which are actually read. It does not however limit the total time you need to retrieve the page.

like image 88
jarnbjo Avatar answered Jul 17 '26 17:07

jarnbjo


You can close the stream from another thread.

like image 31
Tom Hawtin - tackline Avatar answered Jul 17 '26 16:07

Tom Hawtin - tackline



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!