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?
Before anybody comments it, I know that a connect timeout should be set as well.
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.
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.