- 使用 guava retry 工具包
com.github.rholder
guava-retrying
2.0.0
- 简单的 retry 工具类,如下:
public abstract class RetryOperation {
public boolean execute(int retryCount, int waitTime, TimeUnit timeUnit) {
int count = 0;
boolean isSuccess = false;
while (retryCount > count) {
try {
isSuccess = process();
if (isSuccess) {
break;
}
} catch (Exception e) {
LOGGER.info("execute exception", e);
}
try {
timeUnit.sleep(waitTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
count++;
LOGGER.info("retry count {}", count);
}
return isSuccess;
}
public abstract boolean process() throws Exception;