| Author |
TimeOut for HttpURLConnection
|
Amit G Piplani
Ranch Hand
Joined: Jul 21, 2004
Posts: 82
|
|
Hi, Can anyone tell me how to set timeout for java.net.HttpURLConnection? Amit
|
SCJP,SCWCD,SCBCD,SCDJWS1.4, SCDJWS1.5
|
 |
Ryan McGuire
Ranch Hand
Joined: Feb 18, 2005
Posts: 952
|
|
There is a setConnectionTimeout() method in the Java 1.5.0 version of URLConnection (the base class for HttpURLConnection). If you're stuck using an older Java, you may have to play games with multiple Threads, timers, blah blah blah. Is upgrading your version of Java a possibility? Ryan
|
 |
Amit G Piplani
Ranch Hand
Joined: Jul 21, 2004
Posts: 82
|
|
|
I can't move to JDK 1.5, how should i go ahead with Threads? but i don't need to use System.Property, since it will set for all URL's
|
 |
Ryan McGuire
Ranch Hand
Joined: Feb 18, 2005
Posts: 952
|
|
Originally posted by Amit G Piplani: I can't move to JDK 1.5, how should i go ahead with Threads? but i don't need to use System.Property, since it will set for all URL's
Oops, I take it all back. Try setting the sun.net.client.defaultConnectTimeout and/or sun.net.client.defaultReadTimeout properties. See this for more information. For instance, you might start your app like this: ...to make your client wait only a second and a half for a connection. Ryan [ May 23, 2005: Message edited by: Ryan McGuire ]
|
 |
Ryan McGuire
Ranch Hand
Joined: Feb 18, 2005
Posts: 952
|
|
Originally posted by Amit G Piplani: I can't move to JDK 1.5, how should i go ahead with Threads? but i don't need to use System.Property, since it will set for all URL's
Would it be possible to set the property, make the connection request to that one URL, and then set the property back to the default value?
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Wouldn't that be nice? You might get away with it in a single-threaded client. But if you're in a multi-threaded environment you'd have to track threads coming in and out of the method that need the lower value. See the Apache Commons HttpClient package. It has a class compatible with HttpURLConnection that includes timeout. I tried to find solutions for this that I can use in an EJB container and never did. The spec discourages (forbids) playing with threads.
|
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
|
 |
 |
|
|
subject: TimeOut for HttpURLConnection
|
|
|