Skip to content

Commit 3fe3b7f

Browse files
seonWKimfmbenhassine
authored andcommitted
Add ability to start a job flow with a decider
Resolves #4411
1 parent 2e8d506 commit 3fe3b7f

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/FlowJobBuilder.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2011 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import org.springframework.batch.core.Step;
2020
import org.springframework.batch.core.job.flow.Flow;
2121
import org.springframework.batch.core.job.flow.FlowJob;
22+
import org.springframework.batch.core.job.flow.JobExecutionDecider;
2223
import org.springframework.batch.core.step.builder.StepBuilderException;
2324

2425
/**
@@ -61,6 +62,16 @@ public JobFlowBuilder start(Step step) {
6162
return new JobFlowBuilder(this, step);
6263
}
6364

65+
/**
66+
* Start a job with this decider, but expect to transition from there to other flows
67+
* or steps.
68+
* @param decider the decider to start with
69+
* @return a builder to enable fluent chaining
70+
*/
71+
public JobFlowBuilder start(JobExecutionDecider decider) {
72+
return new JobFlowBuilder(this, decider);
73+
}
74+
6475
/**
6576
* Provide a single flow to execute as the job.
6677
* @param flow the flow to execute

Diff for: spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilder.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.springframework.batch.core.Step;
1919
import org.springframework.batch.core.job.flow.Flow;
20+
import org.springframework.batch.core.job.flow.JobExecutionDecider;
2021
import org.springframework.batch.core.repository.JobRepository;
2122

2223
/**
@@ -61,16 +62,25 @@ public SimpleJobBuilder start(Step step) {
6162
/**
6263
* Create a new job builder that will execute a flow.
6364
* @param flow a flow to execute
64-
* @return a {@link SimpleJobBuilder}
65+
* @return a {@link JobFlowBuilder}
6566
*/
6667
public JobFlowBuilder start(Flow flow) {
6768
return new FlowJobBuilder(this).start(flow);
6869
}
6970

71+
/**
72+
* Create a new job builder that will start with a decider.
73+
* @param decider a decider to start with
74+
* @return a {@link JobFlowBuilder}
75+
*/
76+
public JobFlowBuilder start(JobExecutionDecider decider) {
77+
return new FlowJobBuilder(this).start(decider);
78+
}
79+
7080
/**
7181
* Create a new job builder that will execute a step or sequence of steps.
7282
* @param step a step to execute
73-
* @return a {@link SimpleJobBuilder}
83+
* @return a {@link JobFlowBuilder}
7484
*/
7585
public JobFlowBuilder flow(Step step) {
7686
return new FlowJobBuilder(this).start(step);

Diff for: spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/FlowJobBuilderTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,24 @@ public FlowExecutionStatus decide(JobExecution jobExecution, @Nullable StepExecu
235235
assertEquals(2, execution.getStepExecutions().size());
236236
}
237237

238+
@Test
239+
void testBuildWithDeciderAtStart() {
240+
JobExecutionDecider decider = new JobExecutionDecider() {
241+
private int count = 0;
242+
243+
@Override
244+
public FlowExecutionStatus decide(JobExecution jobExecution, @Nullable StepExecution stepExecution) {
245+
count++;
246+
return count < 2 ? new FlowExecutionStatus("ONGOING") : FlowExecutionStatus.COMPLETED;
247+
}
248+
};
249+
JobFlowBuilder builder = new JobBuilder("flow", jobRepository).start(decider);
250+
builder.on("COMPLETED").end().from(decider).on("*").to(step1).end();
251+
builder.build().preventRestart().build().execute(execution);
252+
assertEquals(BatchStatus.COMPLETED, execution.getStatus());
253+
assertEquals(1, execution.getStepExecutions().size());
254+
}
255+
238256
@Test
239257
void testBuildWithIntermediateSimpleJob() {
240258
SimpleJobBuilder builder = new JobBuilder("flow", jobRepository).start(step1);

0 commit comments

Comments
 (0)