简体   繁体   English

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

[英]Spring Batch - execute Steps based on decisions issue

I'm developing Spring Batch where I've Steps-1 to Step-10 which runs sequentially.我正在开发Spring Batch ,其中我有Steps-1Step-10 ,它们按顺序运行。 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.在我的第 4 步到第 7 步的情况下,我必须根据执行Step-X, Step-YStep-Z等做出有条件的决定。

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-4 - 前提条件,如果Step-X给出除错误以外的任何状态,则执行Step-4 ,如果Step-X失败则执行failedStep()步骤。

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

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 . Step-6 - 前提条件,如果Step-Z给出除错误以外的任何状态,则执行Step-6 ,如果Step-z失败则执行failedStep() step 和step-8step-10 I'm looking to do all in a single batch job only.我只想在一个批处理作业中完成所有工作。

I've developed some code, but it looks like it's not liking the condition.我已经开发了一些代码,但看起来它不喜欢这种情况。

Error - The method on(String) is undefined for the type Step错误 - 类型 Step 的 on(String) 方法未定义

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

在此处输入图像描述

I was able to solve the issue using following code - This works perfectly fine for me.我能够使用以下代码解决问题 - 这对我来说非常好。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM