Add ability to start a job flow with a decider

Resolves #4411
This commit is contained in:
Kim Seon Woo
2023-08-03 23:37:31 +09:00
committed by Mahmoud Ben Hassine
parent 2e8d5063f7
commit 3fe3b7fc5d
3 changed files with 42 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2011 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.job.flow.FlowJob;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.step.builder.StepBuilderException;
/**
@@ -61,6 +62,16 @@ public class FlowJobBuilder extends JobBuilderHelper<FlowJobBuilder> {
return new JobFlowBuilder(this, step);
}
/**
* Start a job with this decider, but expect to transition from there to other flows
* or steps.
* @param decider the decider to start with
* @return a builder to enable fluent chaining
*/
public JobFlowBuilder start(JobExecutionDecider decider) {
return new JobFlowBuilder(this, decider);
}
/**
* Provide a single flow to execute as the job.
* @param flow the flow to execute

View File

@@ -17,6 +17,7 @@ package org.springframework.batch.core.job.builder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.repository.JobRepository;
/**
@@ -61,16 +62,25 @@ public class JobBuilder extends JobBuilderHelper<JobBuilder> {
/**
* Create a new job builder that will execute a flow.
* @param flow a flow to execute
* @return a {@link SimpleJobBuilder}
* @return a {@link JobFlowBuilder}
*/
public JobFlowBuilder start(Flow flow) {
return new FlowJobBuilder(this).start(flow);
}
/**
* Create a new job builder that will start with a decider.
* @param decider a decider to start with
* @return a {@link JobFlowBuilder}
*/
public JobFlowBuilder start(JobExecutionDecider decider) {
return new FlowJobBuilder(this).start(decider);
}
/**
* Create a new job builder that will execute a step or sequence of steps.
* @param step a step to execute
* @return a {@link SimpleJobBuilder}
* @return a {@link JobFlowBuilder}
*/
public JobFlowBuilder flow(Step step) {
return new FlowJobBuilder(this).start(step);

View File

@@ -235,6 +235,24 @@ class FlowJobBuilderTests {
assertEquals(2, execution.getStepExecutions().size());
}
@Test
void testBuildWithDeciderAtStart() {
JobExecutionDecider decider = new JobExecutionDecider() {
private int count = 0;
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, @Nullable StepExecution stepExecution) {
count++;
return count < 2 ? new FlowExecutionStatus("ONGOING") : FlowExecutionStatus.COMPLETED;
}
};
JobFlowBuilder builder = new JobBuilder("flow", jobRepository).start(decider);
builder.on("COMPLETED").end().from(decider).on("*").to(step1).end();
builder.build().preventRestart().build().execute(execution);
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
assertEquals(1, execution.getStepExecutions().size());
}
@Test
void testBuildWithIntermediateSimpleJob() {
SimpleJobBuilder builder = new JobBuilder("flow", jobRepository).start(step1);