I'd like to create a generic StepExecutionListener
. It should log the item count processed by a Tasklet
step.
Problem: the count is not updated automatically. So I probably have to do this somewhere in Tasklet.execute()
. But how?
@Bean
@JobScope
public Tasklet myTasklet() {
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
int count = dao.deleteByName(name);
//TODO how to set the count into the stepExecution context?
return RepeatStatus.FINISHED;
}
};
}
@Bean
public Step myStep() {
return getStepBuilderFactory().get("myStep")
.listener(new StepListener())
.tasklet(myTasklet())
.build();
}
public class StepListener extends StepExecutionListenerSupport {
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
long items = stepExecution.getWriteCount();
logger.info("items processed in tasklet: " + items);
return super.afterStep(stepExecution);
}
}