In my program I call a method that can either return a value or "", and basically I want it to retry looking up the value 3 times with a delay of 1 second inbetween each try until it gets a value or the tries are finished.
This is what I currently have but I feel like this solution is pretty gross, particularly having to put the try/catch around the sleep. Does anyone know of a better way to retry with a delay?
public void method(String input){
String value = getValue(input);
int tries = 0
while (value.equals("") && tries < 3){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
log.error("Thread interrupted");
}
value = getValue(input);
tries += 1;
}
return value;
}
InterruptedException. There's a library for these kind of tasks called retrier, but I can't say I've used it. - shmosel