Skip to content

Allow SimpleJobBuilder to start with JobExecutionDecider #4424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;

/**
Expand Down Expand Up @@ -61,6 +62,16 @@ public JobFlowBuilder start(Step step) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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;

/**
Expand Down Expand Up @@ -62,16 +63,25 @@ public SimpleJobBuilder start(Step step) {
/**
* 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 execute a decider.
* @param decider a decider to execute
* @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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ public FlowExecutionStatus decide(JobExecution jobExecution, @Nullable StepExecu
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);
Expand Down