File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Thread timeouts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread timeouts" Watch "Thread timeouts" New topic

Thread timeouts

Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
My Java program polls a URL that returns an XML Document. Here's the code I'm using, to hit the URL and parse the xml contents:

The code block is synchronized because there are more than one instances of the program running, and at any given instant I want only one instance to hit the URL. Periodically, the server hosting the URL is rebooted. When the server is rebooted, occasionally, one of the instances sending the request just hangs, and never returns. Each instance of my program writes to a different log file. All the instances, except one, indicate that they are waiting to acquire the lock, and one instance has acquired the lock, sent the request, but hasn't released the lock. The lock doesn't get released until the process is restarted. Is there a way I can specify a timeout value while trying to hit the URL? Thanks in advance. I am using JDK 1.3.1_02.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
HttpURLConnection doesn't have a timeout parameter. It's a problem for me in my work life. Sheesh. The Apache Commons HTTPClient package has an equivalent class with timeout. I've also seen people pass the connection to a timer command set to run in nnn milliseconds. The command closes the connection which should make your method throw an exception. See if either of those could work for you ... and let me know!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
Thanks for your reponse. I am now using the commons-httpclient API to send HTTP Requests. Tested the timeout feature and it works great. I have to see if it solves my problem in production. Thanks again.
I agree. Here's the link:
subject: Thread timeouts
It's not a secret anymore!