Skip to content

Instantly share code, notes, and snippets.

@morficus
Created April 8, 2014 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morficus/5bd94e330bf4212679b5 to your computer and use it in GitHub Desktop.
Save morficus/5bd94e330bf4212679b5 to your computer and use it in GitHub Desktop.
Retry HTTP call on connection failure in sendHTTPReponse() + "Max retry" exception
.
. .
. . .
} catch (IOException e) {
//If we have connectionRetryLimit set to > 0 then retry that many times.
if( numberOfAttempts <= retryLimit) {
listener.getLogger().println("Connection to remote server failed, waiting for to retry - " + this.pollInterval + " seconds until next attempt.");
// Sleep for 'pollInterval' seconds.
// Sleep takes miliseconds so need to convert this.pollInterval to milisecopnds (x 1000)
try {
// Could do with a better way of sleeping...
Thread.sleep(this.pollInterval * 1000);
} catch (InterruptedException ex) {
this.failBuild(ex, listener);
}
listener.getLogger().println("Retry attempt #" + numberOfAttempts + " out of " + retryLimit );
numberOfAttempts++;
responseObject = sendHTTPCall(urlString, requestType, build, listener, numberOfAttempts);
}else if(numberOfAttempts > retryLimit){
//reached the maximum number of retries, time to fail
this.failBuild(new Exception("Max number of connection retries have been exeeded."), listener);
}else{
//something failed with the connection and we retried the max amount of times... so throw an exception to mark the build as failed.
this.failBuild(e, listener);
}
} finally {
. . .
. .
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment