During a server start-up procedure, I access a remote host for some initialisation data. Unfortunately, the remote host in question is notorious for being randomly unresponsive to connections (lets say 3% of the time this happens). The result is that the connection hangs indefinitely (possibly forever), and my server doesn't start up in a reasonable amount of time, if at all. It's hung for an hour before.
A connection timeout:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(connectionTimeout);
for (int i = 0; i <= connectionRetries; i++) {
try {
log.info("Establishing connection to ClassNotes service.");
conn.connect();
break;
} catch (SocketTimeoutException e) {
log.warn("Timed out connecting to ClassNotes service.");
}
}
does not work, since the connection is already established with the server when it hangs. The problem seems to be when it tries to get an InputStream from the server.
For any one who has had to work with disobediant services, should I thread the process of getting the InputStream with a finite number of retries, and just deal with one or two potential threads hanging in the JVM's memory indefinitely? Or, is there a more common way to handle this in Java?
It could also be useful to know that a server starting up without the information from the remote host is not as devastating as the server not starting up at all.
Thanks in advance.