如何有条件地跳过Spring Batch步骤?

时间:2018-09-29 21:36:00

标签: spring kotlin spring-batch

我有一个前提条件步骤,测试流程和后置条件步骤。如果前提条件步骤失败,我想跳过测试流程。在文档Configuring a step无法正常工作之后,无论执行什么测试流程都将​​被执行。我想念什么? 使用Spring Boot 2.0.5.RELEASE和Spring Batch 4.0.1.RELEASE。

val testingFlow = FlowBuilder<SimpleFlow>("TESTING_FLOW")
    .start(testExecutorDecider)
    .on(TestExecutor.JUNIT.name).to(junitExecutionStep())
    .from(testExecutorDecider).on(TestExecutor.GRADLE.name).to(gradleExecutionStep())
    .end()

return jobs.get(touchstoneProperties.jobName)
    .start(preConditionsStep())
    .on("*").to(testingFlow)
    .from(preConditionsStep()).on("FAILED").to(postConditionsStep())
    .from(testingFlow).on("*").to(postConditionsStep())
    .end()
    .build()

1 个答案:

答案 0 :(得分:0)

我自己弄清楚了。

val testingFlow = FlowBuilder<SimpleFlow>("TESTING_FLOW")
    .start(testExecutorDecider)
    .on(TestExecutor.JUNIT.name).to(junitExecutionStep())
    .from(testExecutorDecider).on(TestExecutor.GRADLE.name).to(gradleExecutionStep())
    .from(testExecutorDecider).on("*").end()
    .build()

return jobs.get(touchstoneProperties.jobName)
    .start(preConditionsStep())
    .on("*").to(testingFlow)
    .from(testingFlow).on("*").to(postConditionsStep()).end()
    .build()

TestExecutorDecider

class TestExecutorDecider(private val touchstoneProperties: TouchstoneProperties) : JobExecutionDecider {
    companion object {
        private val LOGGER = LoggerFactory.getLogger(TestExecutorDecider::class.java)
    }

    override fun decide(jobExecution: JobExecution, stepExecution: StepExecution): FlowExecutionStatus {
        return when (stepExecution.exitStatus.compareTo(ExitStatus.FAILED)) {
            0 -> FlowExecutionStatus("SKIPPED")
            else -> {
                LOGGER.info("Test executor: {}", touchstoneProperties.testExecutor)
                return FlowExecutionStatus(touchstoneProperties.testExecutor.name)
            }
        }
    }
}