Dupliquer l'étape [step2] détectée lors de l'exécution du job = [job]. Si l'une des étapes échoue, les deux seront exécutées à nouveau au redémarrage

Dec 08 2020

Le décideur Spring Batch entre dans forloop. J'ai ci-dessous les exigences.

Si Step1exécuter, vérifier decider () si "NO"puis terminer Job, si "Yes"ensuite exécuter Step2, si Step2 est TERMINE puis exécuter decider () f "NO"puis terminer Job, si "Yes"ensuite exécuter Step3.

Des conseils comment pouvons-nous configurer dans le lot?

2020-12-08 11:41:11.473  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
step1
2020-12-08 11:41:11.493  INFO 16800 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step1] executed in 20ms
2020-12-08 11:41:11.508  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step2]
step2
2020-12-08 11:41:11.513  INFO 16800 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step2] executed in 5ms
2020-12-08 11:41:11.568  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step2] detected in execution of job=[job]. If either step fails, both will be executed again on restart.
2020-12-08 11:41:11.571  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step2]
step2
2020-12-08 11:41:11.577  INFO 16800 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step2] executed in 6ms
2020-12-08 11:41:11.585  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step2] detected in execution of job=[job]. If either step fails, both will be executed again on restart.
2020-12-08 11:41:11.589  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step2]
step2
2020-12-08 11:41:11.594  INFO 16800 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step2] executed in 5ms
2020-12-08 11:41:11.601  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step2] detected in execution of job=[job]. If either step fails, both will be executed again on restart.
2020-12-08 11:41:11.604  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step2]
step2
2020-12-08 11:41:11.608  INFO 16800 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step2] executed in 3ms
2020-12-08 11:41:11.616  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step2] detected in execution of job=[job]. If either step fails, both will be executed again on restart.
2020-12-08 11:41:11.618  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step2]
step2
2020-12-08 11:41:11.623  INFO 16800 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step2] executed in 5ms
2020-12-08 11:41:11.630  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step2] detected in execution of job=[job]. If either step fails, both will be executed again on restart.
2020-12-08 11:41:11.634  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step2]
step2
2020-12-08 11:41:11.638  INFO 16800 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step2] executed in 4ms
2020-12-08 11:41:11.646  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Duplicate step [step2] detected in execution of job=[job]. If either step fails, both will be executed again on restart.
2020-12-08 11:41:11.648  INFO 16800 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step2]
step2

Code Java

@Configuration
public class Config {
    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Bean
    public Step step1() {
        return steps.get("step1")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("step1");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public JobExecutionDecider decider() {
        return (jobExecution, stepExecution) -> new FlowExecutionStatus("SUCCESS"); // or NO
    }

    @Bean
    public Step step2() {
        return steps.get("step2")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("step2");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Step step3() {
        return steps.get("step3")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("step3");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
    
    
    @Bean
    public Step step5() {
        return steps.get("step5")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("Step 5");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
    
    
    @Bean
    public Step step4() {
        return steps.get("step4")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("Step 4");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Job job() {
        return jobs.get("job")
                .incrementer(new RunIdIncrementer())
                .start(step1())
                .next(decider())
                .from(decider()).on("SUCCESS").to(step2())
                .from(decider()).on("NO").end()
                .from(step2()).on("COMPLETED").to(decider())
                        .from(decider()).on("SUCCESS").to(step3())
                        .from(decider()).on("NO").end()
                .end()
                .build();
    }
}

Remarque - En raison de restrictions de sécurité, je ne peux pas charger le diagramme de flux à partir du poste de travail de bureau :(

Réponses

MahmoudBenHassine Dec 09 2020 at 18:00

Vous avez un bogue dans votre définition de flux. Le SUCCESSrésultat du décideur passe en deux étapes différentes:

.from(decider()).on("SUCCESS").to(step2())

...

.from(decider()).on("SUCCESS").to(step3())

De plus, les définitions de transition de step2sont incomplètes. Vous avez uniquement défini la transition à partir de l'étape 2 COMPLETED:

.from(step2()).on("COMPLETED").to(decider())

vous devez également définir la transition à partir step2des autres cas.