I configured the CustomChunkListener. I created to check the status of redis Distributed lock and throw an Custom Exception if it is locked.
In Step, of Custom Exception is caught with fault Tolerant, Step is configured to be re-executed up to 3 times.
However, FaultTolerantStepBuilder delegates ChunkListener to TerminateOnExceptionChunkListenerDelegate class and throws FatalStepExecutionException. Unable to catch CustomException
my code sample.
public Class MyBatchConfig {
public Step MyBatchStep() {
return stepBuilderFactory.get("refineCarStatusStep")
.<I, O>chunk(CHUNK_SIZE)
.reader(MyItemReader())
.processor(MyItemProcessor())
.writer(MyItemWriter())
.faultTolerant()
.retry(CustomException.class)
.retryLimit(3)
.listener(new CustomChunkListener())
.build();
}
}
public class CustomChunkListener implements ChunkListener {
@Override
public void beforeChunk(ChunkContext context) {
// example. redis Distributed lock check
if(isLocked) {
throw new CustomException();
}
}
@Override
public void afterChunk(ChunkContext context) {
unlock();
}
@Override
public void afterChunkError(ChunkContext context) {
unlock();
}
}
How can I throw, catch and retry an exception before the start of the chunk?