ndee
ndee

Reputation: 335

Howto make a function "timeoutable" in Java?

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!

Upvotes: 3

Views: 1130

Answers (5)

matt b
matt b

Reputation: 139921

Google's recently released guava-libraries have some classes that offer similar functionality:

TimeLimiter:

Produces proxies that impose a time limit on method calls to the proxied object. For example, to return the value of target.someMethod(), but substitute DEFAULT_VALUE if this method call takes over 50 ms, you can use this code ...

Upvotes: 3

jarnbjo
jarnbjo

Reputation: 34313

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.

Upvotes: 7

pgras
pgras

Reputation: 12770

Have a look at FutureTask...

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328546

Try to interrupt the thread; many blocking calls in Java will continue when they receive an interrupt.

In this case, content should be empty and Thread.isInterrupted() should be true.

Upvotes: 0

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147124

You can close the stream from another thread.

Upvotes: 3

Related Questions