我正在研究 Spring Batch Retry机制。我已经实现了以下代码,并将FixedBackOffPolicy
设置为每10秒启动一次重试机制。
但是,当我对此进行测试时,发现下面的FixedBackOffPolicy
backOffPolicy.setBackOffPeriod((Long.valueOf(retryPeriod) * 1000))
代码无法正常工作。它没有考虑回退超时(我假设这是两次重试计数之间的持续时间)。
public class ConnectionRetryCallback implements RetryCallback {
private static Logger logger = Logger.getLogger(ConnectionRetryCallback.class);
@Autowired
private ABCBatchFilesTasklet tasklet;
private Set<String> abcFileNames;
private ChunkContext paramChunkContext;
public ConnectionRetryCallback(Set<String> abcFileNames, ABCBatchFilesTasklet tasklet, ChunkContext paramChunkContext) {
this.abcFileNames = abcFileNames;
this.tasklet = tasklet;
this.paramChunkContext = paramChunkContext;
}
@Override
public Object doWithRetry(RetryContext retryContext) throws Throwable {
logger.debug("ConnectionRetryCallback | doWithRetry");
return tasklet.executeShellScript(abcFileNames, paramChunkContext);
}
}
从status = 2开始的位置,调用以下逻辑。在我的情况下,status = 2,假定为失败并调用重试机制。
retryCount++;
ConnectionRetryCallback callback = new ConnectionRetryCallback(abcFileNames, this, paramChunkContext);
try {
long lStartTime = System.nanoTime();
if(retryCount <= Integer.valueOf(retryshellscriptExeCount)) {
logger.info("Retry Mechanism to be applied ="+!MapUtils.isEmpty(fileSendStatusMap));
logger.info("Retry toABC File, Retry Count = " + retryCount+", Retry to send these files ="+abcFileNames);
final FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod((Long.valueOf(retryPeriod) * 1000)); // 1 sec = 1000 millisec
retryTemplate.setBackOffPolicy(backOffPolicy);
retryTemplate.execute(callback);
}else {
logger.info("Retry Consumed");
}
} catch (Throwable e) {
logger.error(e.getMessage());
}