无法使 spring 批处理条件流程正常工作

Can't get spring batch conditional flows working

我在使用 java 配置使条件 spring 批处理流工作时遇到问题。我在 spring 批处理示例或 spring 批处理的测试代码或堆栈溢出中看到的示例倾向于显示条件,其中单个步骤需要在条件下执行,或者这是最后一步, 或两者。这不是我需要解决的问题。

在过程伪代码中,我希望它表现得像

initStep()
if decision1()
    subflow1()
middleStep()
if decision2()
    subflow2()
lastStep()

因此,subflow1 和 2 是有条件的,但 init、middle 和 last 始终执行。这是我精简的测试用例。在当前配置中,它只是在执行 subflow1 后退出。

public class FlowJobTest {

private JobBuilderFactory jobBuilderFactory;
private JobRepository jobRepository;
private JobExecution execution;

@BeforeMethod
public void setUp() throws Exception {
    jobRepository = new MapJobRepositoryFactoryBean().getObject();
    jobBuilderFactory = new JobBuilderFactory(jobRepository);
    execution = jobRepository.createJobExecution("flow", new JobParameters());
}

@Test
public void figureOutFlowJobs() throws Exception {

    JobExecutionDecider subflow1Decider = decider(true);
    JobExecutionDecider subflow2Decider = decider(false);

    Flow subflow1 = new FlowBuilder<Flow>("subflow-1").start(echo("subflow-1-Step-1")).next(echo("subflow-1-Step-2")).end();
    Flow subflow2 = new FlowBuilder<Flow>("subflow-2").start(echo("subflow-2-Step-1")).next(echo("subflow-2-Step-2")).end();

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

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

            .next(echo("last"))
            .build().preventRestart().build();

    job.execute(execution);
    assertEquals(execution.getStatus(), BatchStatus.COMPLETED);
    assertEquals(execution.getStepExecutions().size(), 5);
}


private Step echo(String stepName) {
    return new AbstractStep() {
        {
            setName(stepName);
            setJobRepository(jobRepository);
        }
        @Override
        protected void doExecute(StepExecution stepExecution) throws Exception {
            System.out.println("step: " + stepName);
            stepExecution.upgradeStatus(BatchStatus.COMPLETED);
            stepExecution.setExitStatus(ExitStatus.COMPLETED);
            jobRepository.update(stepExecution);
        }
    };
}

private JobExecutionDecider decider(boolean decision) {
    return (jobExecution, stepExecution) -> new FlowExecutionStatus(decision ? "YES" : "NO");
}

}

我用来完成这项工作的方法是将我的条件流程分解为流程步骤。

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

您的原始工作定义应该也可以工作,只需稍作调整即可。测试失败是因为作业在第一个子流程之后完成(状态为 COMPLETED)。如果您指示它继续执行中间步骤,它应该会按预期工作。第二个流量的类似调整。

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