Spring 批处理 - 根据决策问题执行步骤

Spring Batch - execute Steps based on decisions issue

我正在开发 Spring Batch,其中我 Steps-1Step-10 按顺序运行。在我的第 4 步到第 7 步的情况下,我必须根据我执行的条件做出 Step-X, Step-YStep-Z

Ex: Step-4 - Precondition, 如果 Step-X 给出除错误之外的任何状态,则执行 Step-4,如果 Step-X 失败则执行 failedStep() 步骤。

Step-5 - 前提条件,如果 Step-Y 给出错误以外的任何状态,则执行 Step-5,如果 Step-Y 失败则执行 failedStep() 步骤。

Step-6 - 前提条件,如果 Step-Z 给出错误以外的任何状态,则执行 Step-6,如果 Step-z 失败则执行 failedStep() 步骤和 step-8step-10。我只想在一个批处理作业中完成所有工作。

我已经开发了一些代码,但它看起来不喜欢这种情况。

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();
    }
}

我能够使用以下代码解决问题 - 这对我来说非常有效。

方法一

@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();
}