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.
Joined: Jan 29, 2003
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
Joined: Jan 26, 2001
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.