tags:

views:

40

answers:

2

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.

+2  A: 

You can setReadTimeout() in addition to the connection timeout.

Mirko Nasato
D'oh! Don't know how I missed that API method. I think this is a logical step before resorting to threading. Thanks!
BranTheMan
+1  A: 

You could put this logic in a Runnable object, then create a Thread that contacts the service. If the Thread hasn't finished in a specified amount of time, then kill that Thread and try again. Do this until it works or until you've reached the retry limit, and then continue on with startup.

Kaleb Brasee
Thanks Kaleb. Depending if setReadTimeout() works, I might have to resort to this.
BranTheMan
Ah man, I forgot all about setReadTimeout! I even used it on a client project before too, LOL.
Kaleb Brasee