Can't get spring batch conditional flows working

北城余情 提交于 2019-12-05 12:15:30

Your original job definition should work, as well, with only a small tweak. The test was failing because the job finished (with status COMPLETED) after the first sub flow. If you instruct it to continue to the middle step instead, it should work as intended. Similar adjustment for the second flow.

Job job = jobBuilderFactory.get("testJob")
        .start(echo("init"))

        .next(subflow1Decider)
            .on("YES").to(subflow1).next(echo("middle"))
        .from(subflow1Decider)
            .on("*").to(echo("middle"))
        .next(subflow2Decider)
            .on("YES").to(subflow2).next(echo("last"))
        .from(subflow2Decider)
            .on("*").to(echo("last"))

        .build().preventRestart().build();

The approach I used to make this work was to break my conditional flows into flow steps.

    public void figureOutFlowJobsWithFlowStep(boolean decider1, boolean decider2, int expectedSteps) throws Exception {

    JobExecutionDecider subflow1Decider = decider(decider1);
    JobExecutionDecider subflow2Decider = decider(decider2);

    Flow subFlow1 = new FlowBuilder<Flow>("sub-1")
            .start(subflow1Decider)
            .on("YES")
            .to(echo("sub-1-1")).next(echo("sub-1-2"))
            .from(subflow1Decider)
            .on("*").end()
            .end();
    Flow subFlow2 = new FlowBuilder<Flow>("sub-2")
            .start(subflow2Decider)
            .on("YES").to(echo("sub-2-1")).next(echo("sub-2-2"))
            .from(subflow2Decider)
            .on("*").end()
            .end();

    Step subFlowStep1 = new StepBuilder("sub1step").flow(subFlow1).repository(jobRepository).build();
    Step subFlowStep2 = new StepBuilder("sub2step").flow(subFlow2).repository(jobRepository).build();

    Job job = jobBuilderFactory.get("testJob")
            .start(echo("init"))
            .next(subFlowStep1)
            .next(echo("middle"))
            .next(subFlowStep2)
            .next(echo("last"))
            .preventRestart().build();

    job.execute(execution);
    assertEquals(execution.getStatus(), BatchStatus.COMPLETED);
    assertEquals(execution.getStepExecutions().size(), expectedSteps);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!