基于问题决策执行步骤

nom7f22z  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(269)

我正在开发spring批 Steps-1Step-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-8step-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();
    }
}

wvyml7n5

wvyml7n51#

我可以用下面的代码解决这个问题-这对我来说非常好。
方法-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();
}

相关问题