Skip to content

Commit

Permalink
Add ability to start a job flow with a decider
Browse files Browse the repository at this point in the history
Resolves #4411
  • Loading branch information
seonWKim authored and fmbenhassine committed Sep 8, 2023
1 parent 2e8d506 commit 3fe3b7f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 @@ -61,16 +62,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 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);
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

0 comments on commit 3fe3b7f

Please sign in to comment.