Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread timeouts

 
Bala Krishna
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bala Krishna
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic