spring - 无法使 Spring Batch 条件流工作

标签 spring spring-batch

我在使用 java 配置让条件 Spring 批处理流程工作时遇到问题。我在 Spring Batch 示例、Spring Batch 测试代码或堆栈溢出中看到的示例往往显示一个条件,其中需要根据条件执行单个步骤,或者它是最后一步,或两者兼而有之。这不是我需要解决的问题。

在程序伪代码中,我希望它的行为类似于

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

}

最佳答案

您最初的职位定义应该也能发挥作用,只需稍作调整即可。测试失败,因为作业在第一个子流之后完成(状态为 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();

关于spring - 无法使 Spring Batch 条件流工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340535/

相关文章:

java - 在 thymeleaf 中使用 Java 和 Spring 时,如何包含我的 css 文件?

spring - Spring 型材的替代品

java - 如何将JOOQ与SpringBatch JdbcTemplate同步

java - 如何在 Spring Batch 中链接多个 ItemReader?

java - 如何自动加载 servlet-context.xml?

java - 所有 Spring 管理的 Bean 的列表

java - 有没有类似 Spring TestExecutionListener for TestSuite 的东西?

grails - Grails 2.5.0:自动填充数据库的方式

java - Spring Batch-集成复用组件

java - Spring 批处理 JobRepository 位置和缩放