I am trying to stop a running job based on timeout value. I am following a post found here, but I am not sure how you add this listener.
Here is the listener implementation
public class StopListener extends StepListenerSupport{
public static final Logger LOG = LoggerFactory.getLogger(StopListener.class);
private static final int TIMEOUT = 30;
private StepExecution stepExecution;
@Override
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
@Override
public void afterChunk(ChunkContext context) {
if (timeout(context)) {
this.stepExecution.setTerminateOnly();
}
}
private boolean timeout(ChunkContext chunkContext) {
LOG.info("----- TIMEOUT-----");
Date startTime = chunkContext.getStepContext().getStepExecution().getJobExecution().getStartTime();
Date now = new Date();
return Duration.between(startTime.toInstant(), now.toInstant()).toMinutes() > TIMEOUT;
}
}
Here is my step
@Bean
public Step dataFilterStep() {
return stepBuilderFactory.get("dataFilterStep")
.<UserInfo, UserInfo> chunk(10)
.reader(dataFilterItemReader())
.processor(dataFilterItemProcessor())
.writer(dataFilterWriter())
.listener(new StopListener())
.build();
}
But I am getting error saying "The method listener(Object) is ambiguous for the type SimpleStepBuilder<UserInfo,UserInfo>". A help would be really appreciated!