I am trying to write a retry mechanism in java, that reties a function after 3 minutes in case of failure, and retries up to maximum 3 times. I do not want to use Thread.Sleep, and instead I was thinking about using ScheduledExecutorService. I am trying to figure out what would be a good implementation for it. It seems executor.schedule() does not the runnable inside the runnable.
I was thinking something like this:
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
final int count = 1;
final int MAX_RETRY = 3;
Runnable runnable = () -> {
try {
//This function can fail and throw FunctionException
callMyFunction();
}
catch (FunctionException e) {
if (++count <= MAX_RETRY) {
executor.schedule(runnable, 30*60*1000);
}
}
};
executor.execute(runnable);