I'm developing Spring Batch where I've Steps-1
to Step-10
which runs sequentially. In my case for step-4 to Step-7, I've to make conditional decision based on which I've execute Step-X, Step-Y
and , Step-Z
etc.
Ex: Step-4 - Precondition, if Step-X
gives any status other than error, then execute Step-4
, if Step-X
failed then execute failedStep()
step.
Step-5 - Precondition, if Step-Y
gives any status other than error, then execute Step-5
, if Step-Y
failed then execute failedStep()
step.
Step-6 - Precondition, if Step-Z
gives any status other than error, then execute Step-6
, if Step-z
failed then execute failedStep()
step and the step-8
to step-10
. I'm looking to do all in a single batch job only. Any help?
I've developed a code but looks like its not liking the condition?
Error - The method on(String) is undefined for the type Step
@Configuration
public class JobConfig {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Autowired
private JdbcTemplate jdbcTemplate;
@Bean
public Step step1() {
return steps.get("step1")
.tasklet((StepContribution contribution, ChunkContext chunkContext) -> {
System.out.println("Step1");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step2() {
return steps.get("step2")
.tasklet((StepContribution contribution, ChunkContext chunkContext) -> {
System.out.println("step2");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step3() {
return steps.get("step3")
.tasklet((StepContribution contribution, ChunkContext chunkContext) -> {
System.out.println("step3");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step4() {
return steps.get("step4")
.tasklet((StepContribution contribution, ChunkContext chunkContext) -> {
System.out.println("step4");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step5() {
return steps.get("step5")
.tasklet((StepContribution contribution, ChunkContext chunkContext) -> {
System.out.println("step5");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step stepX() {
return steps.get("stepX")
.tasklet((StepContribution contribution, ChunkContext chunkContext) -> {
System.out.println("step4Check");
// int update = jdbcTemplate.update("Query", "SBC"); // Perform some meaningful DB operations
int update = 1; // To simulate issue locally !
if(update == 1) {
chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("COMPLETED"));
}else {
chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("FAILED"));
}
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step stepY() {
return steps.get("stepY")
.tasklet((StepContribution contribution, ChunkContext chunkContext) -> {
System.out.println("step4Check");
// int update = jdbcTemplate.update("Query", "XYZ"); // Perform some meaningful DB operations
int update = 1; // To simulate issue locally !
if(update == 1) {
chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("COMPLETED"));
}else {
chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("FAILED"));
}
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step failedStep() {
return steps.get("failedStep")
.tasklet((contribution, chunkContext) -> {
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Job job() {
return jobs.get("job")
.start(step1())
.next(step2())
.next(step3())
.next(stepX().on(ExitStatus.FAILED.getExitCode()).to(failedStep())
.from(stepX()).on("*").to(step4())
.from(stepY()).on(ExitStatus.FAILED.getExitCode()).to(failedStep())
.from(stepY()).on("*").to(step5())
.build();
}
}
question from:
https://stackoverflow.com/questions/65916260/spring-batch-execute-steps-based-on-decisions-issue 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…