• Post Reply Bookmark Topic Watch Topic
  • New Topic

Immediate read timeout

 
Philip Grove
Ranch Hand
Posts: 68
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can see the standard Java classes does not give the option of retrying a connection a number of times and setting timeout. Since I need that and at several place in my project I have defined a static method that looks like this.



A typical call comes with count=10 (I know it's quite high but that's beside the point), wait=5000 and timeout=10000. I have noticed that if the first attempt ends in a timeout the following attempts have a read timeout of 0 which cause them to timeout immediately.

If the call specifies count <= 0 no retries take place, but because of the System.out.println line it can easily be seen that it actually falls into the right branch of the if statement.

Does anybody here have any idea why the read timeout gets set to 0 (and it doesn't mean infinity in this case) once a timeout has occurred? And more importantly any better ideas to work around this issue than to create a new URLConnection for every attempt?

Edit:
Before anybody comments it, I know that a connect timeout should be set as well.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not tried myself but according to the HttpURLConnection documentation, which I assume you are using because URLConnection is abstract:
Each HttpURLConnection instance is used to make a single request
I read this to mean that you can only make one getInputStream() call and then you have to get another URLConnection. No? Of course it doesn't say one _successful_ request...
 
Philip Grove
Ranch Hand
Posts: 68
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I use the URLConnection class and get the instance when calling url.openConnection(Proxy.NO_PROXY) in line 5. HttpURLConnection is abstract as well and provides functionality I don't need so I have no reason to try and get an instance of it. Nobody ever said (correctly) that you can't use abstract class, creating an instance just has to be done by factory methods and not by direct construction.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstract class can never be created, whether you use a factory or not. The factory is returning an instance of another class that extends URLConnection. Try printing connection.getClass().getName() and take a look at the documentation for that class. It may provide more information. Just because you are calling a method that URLConnection defines doesn't mean that you are using URLConnection's implementation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!