我正在开发Spring批处理,其中有Steps-1到Step-10,它按顺序运行。在我的例子中,步骤4到步骤7,我必须做出有条件的决定,在此基础上执行Step-X, Step-Y和Step-Z等。
步骤4-前提条件,如果Step-X给出了错误以外的任何状态,那么执行Step-4,如果Step-X失败,那么执行failedStep()步骤。
步骤5-前提条件,如果Step-Y给出了错误以外的任何状态,那么执行Step-5,如果Step-Y失败,那么执行failedStep()步骤。
步骤6-前提条件,如果Step-Z给出了错误以外的任何状态,那么执行Step-6,如果Step-z失败,则执行failedStep()步骤和step-8到step-10。我只想在一个批处理的工作中完成所有的工作。
我已经开发了一些代码,但看起来它不喜欢这种情况。
错误-对类型步骤(字符串)上的方法未定义
@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();
}
}

发布于 2021-01-28 04:51:17
我能够用下面的代码来解决这个问题--这对我来说非常好。
方法-1
@Bean
public Job job() {
return jobs.get("job")
.start(step1())
.next(step2())
.next(step3()).on(ExitStatus.FAILED.getExitCode()).to(failedStep())
.from(step3()).on("*").to(stepX())
.from(stepX()).on(ExitStatus.FAILED.getExitCode()).to(failedStep())
.from(stepX()).on("*").to(step4())
.from(step4()).on(ExitStatus.FAILED.getExitCode()).to(failedStep())
.from(step4()).on("*").to(stepY())
.from(stepY()).on(ExitStatus.FAILED.getExitCode()).to(failedStep())
.from(stepY()).on("*").to(step5())
.next(step6())
.build()
.build();
}方法-2
@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())
.next(stepY()).on(ExitStatus.FAILED.getExitCode()).to(failedStep())
.from(stepY()).on("*").to(step5())
.next(step6())
.build()
.build();
}https://stackoverflow.com/questions/65916260
复制相似问题