네트워크 연결 문제로 인해이 오류가 임의로 발생하지만 일반적으로 여러 번 다시 시도하면이 오류가 해결됩니다.
/** An interface where inside the execute() method I call Hector */
public interface Retriable<T> {
T execute();
}
/**
* Executes operation and retries N times in case of an exception
* @param retriable
* @param maxRetries
* @param <T>
* @return
*/
public static <T> T executeWithRetry(Retriable<T> retriable, int maxRetries) {
T result;
int retries = 0;
long sleepSec = 1;
// retry in case of an exception:
while (true) {
try {
result = retriable.execute();
break;
} catch (Exception e) {
if (retries == maxRetries) {
LOG.error("Exception occurred. Reached max retries.", e);
throw e;
}
retries++;
LOG.error(String.format("Exception occurred. Retrying in %d seconds - #%d", sleepSec, retries), e);
try {
Thread.sleep(sleepSec * 1000);
// increase sleepSec exponentially:
sleepSec *= 2;
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
return result;
}
그리고 그것을 사용하는 방법에 대한 예 : : 여기 헥터 API 함수를 재 시도하는 데 사용하는 코드입니다
ColumnFamilyResult<String, String> columns = executeWithRetry(new Retriable<ColumnFamilyResult<String, String>>() {
@Override
public ColumnFamilyResult<String, String> execute() {
return template.queryColumns(row.getKey());
}
});