You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
698 B
30 lines
698 B
package w11;
|
|
|
|
public class RetryUtils {
|
|
|
|
|
|
private static final long BASE_DELAY_MS = 500;
|
|
|
|
@FunctionalInterface
|
|
public interface RetryTask<T> {
|
|
T run() throws Exception;
|
|
}
|
|
|
|
|
|
public static <T> T retry(int maxRetries, RetryTask<T> task) throws Exception {
|
|
int attempt = 0;
|
|
while (true) {
|
|
try {
|
|
return task.run();
|
|
} catch (Exception e) {
|
|
if (attempt >= maxRetries) {
|
|
throw e;
|
|
}
|
|
|
|
long delay = BASE_DELAY_MS * (1L << attempt);
|
|
Thread.sleep(delay);
|
|
attempt++;
|
|
}
|
|
}
|
|
}
|
|
}
|