I am trying to skip the remove and insert steps based on the condition in the deletion Tasklet. For example, when executing removeDepartmentStep, if hasDocuments value in the tasklet is true, then skip and proceed with removeEmployeesStep without “FAILED” job status.
JobConfog:
@Bean
public Job demoJob() {
return new JobBuilder("demoJob", jobRepository)
.incrementer(new RunIdIncrementer())
.start(removeDepartmentStep())
.next(insertDepartmentStep())
.next(removeEmployeesStep())
.next(insertEmployeesStep())
.build();
}
Tasklet:
@Component
@StepScope
public class CommonDeletionTasklet implements Tasklet, StepExecutionListener {
private final MongoTemplate mongoTemplate;
private String collectionName;
private StepExecution stepExecution;
private final boolean skipIfDataExists;
public CommonDeletionTasklet(MongoTemplate mongoTemplate) {
this(mongoTemplate, false);
}
public CommonDeletionTasklet(MongoTemplate mongoTemplate,
boolean skipIfDataExists) {
this.mongoTemplate = mongoTemplate;
this.skipIfDataExists = skipIfDataExists;
}
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
// ...
}
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) {
// if hasDocuments = true then skip the current step and proceed with the nex step
if (hasDocuments) {
stepExecution.setExitStatus(ExitStatus.NOOP); // Set exit status to indicate skipping
return RepeatStatus.FINISHED;
}
// proceed to deletion
return RepeatStatus.FINISHED;
}
@AfterStep
public ExitStatus afterStep(StepExecution stepExecution) {
return ExitStatus.COMPLETED;
}
}
I tried something a shown below, but it does not proceed with the next job and juts finalized the job as COMPLETED:
return new JobBuilder("demoJob", jobRepository)
.incrementer(new RunIdIncrementer())
.start(removeDepartmentStep())
.on("COMPLETED").to(insertDepartmentStep())
.next(removeEmployeesStep())
.on("NOOP").end() // End job if NOOP
.from(removeEmployeesStep())
.on("COMPLETED").to(insertEmployeesStep())
.end() // End job after the last step
.build();
How to provide this config for the explained scenario?
2