Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Timeout for HttpURLConnection  RSS feed

 
Dan Beranga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our website connects to an external site using java.net.HttpURLConnection. The problem is whenever these connection threads experience problems (for whatever reason) the application-server slowly grinds to a halt as it's resources slowly get consumed.
In short I need an implementation of this class that allows me to set the timeout so I can guarantee this sort of thing doesn't happen.
Any ideas?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked into the java.nio classes? Maybe something there can help.
 
Dan Beranga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately we are on JDK 1.30. I think java.nio is part of 1.4
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so if you don't have J2SE1.4 then you need to simulate it. You need to put every read in its own thread with another thread that watches that thread. When the read thread completes, it needs to let the watching thread know that it is done. If the watching thread doesn't get notified in x seconds, then it kills the read thread.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at Swing Timer class, you can use it to make anything timeout, not necessary GUI.
As far as resource slowly comsume out, that is your real problem, try to use OptimizeIt or some other profiler such as -Xrunhprof JVM build-ins to find out the culprits.
Good luck!
 
Dan Beranga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Thomas and Roseanne for your replies. I am also looking at java.util.Timer and TimerTask to may be help me achieve the effect you described Thomas.
ie. set off a TimerTask to execute for a certain time, then set off a thread that makes the httpurlconnection. Whichever thread completes first cancels the other one.
Does this sound reasonable?
 
Sean Sullivan
Ranch Hand
Posts: 427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to set a timeout value, I recommend the Jakarta HttpClient
http://jakarta.apache.org/commons/httpclient/
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!