Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save onefoursix/e30b432871c8ce0ef4af1fd7484dca13 to your computer and use it in GitHub Desktop.
Save onefoursix/e30b432871c8ce0ef4af1fd7484dca13 to your computer and use it in GitHub Desktop.
StreamSets Groovy script retry on unknownHostException
In the Groovy Stage's init script, look for this line of code (around line 138):
// Execute the request
HttpResponse response = httpClient.execute(request)
Comment out that line and replace that line with this section of code:
// mbrooks 06/01/2024 -- comment out this line
// HttpResponse response = httpClient.execute(request)
// mbrooks 06/01/2024 -- begin section to add
HttpResponse response
// Retry counter for unknownHostException
int uheRetryCounter = 0
// Number of retries if we get an unknownHostException
int uheMaxRetries = 5
// Sleep interval in ms between unknownHostException retries
int uheSleepInterval = 1000
// Flag to keep retrying in case we get an unknownHostException
boolean httpClientExecuteIsComplete = false
// Loop around the httpClient.execute call until we do not get an UnknownHostException
// Typically, the loop will exit after the initial call
while (!httpClientExecuteIsComplete && uheRetryCounter < uheMaxRetries){
try {
// execute the HTTP request
response = httpClient.execute(request)
// If we get to this line of code, we did not get an unknownHostException
// so set httpClientExecuteIsComplete to true to exit the retry loop
httpClientExecuteIsComplete = true
} catch (UnknownHostException uhe){
sdc.log.error("Got an UnknownHostException in SaferPayments Groovy script ")
sdc.log.error(uhe.toString(), uhe)
// Increment the retry counter
uheRetryCounter++
// If we have not reached max retries, sleep and stay in the loop
if (uheRetryCounter < uheMaxRetries){
// Calculate sleep interval to wait longer between each retry
int uheSleepMillis = uheSleepInterval * uheRetryCounter
// Sleep
sdc.log.info("Will sleep for " + uheSleepMillis + " millis after unknownHostException")
Thread.sleep(uheSleepMillis)
// Log the retry
sdc.log.info("Will retry httpClient.execute command after unknownHostException")
sdc.log.info("unknownHostException retry # " + uheRetryCounter)
// Rethrow the unknownHostException if we have exceeded max retries
} else {
sdc.log.info("Exceeded max retries of httpClient.execute command after unknownHostException")
throw(uhe)
}
}
}
// mbrooks 06/01/2024 -- end section to add
            
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment