spring-projects / spring-projects/spring-batch

Not unique steps names in a FlowJob should raise an exception or at least log a warning

Open
#3,757 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: core related-to: flow-definition type: enhancement
Dominant language
Java
Stars
3k
Forks
2.5k
Avg merge
6d 53m
Merged PRs (30d)
3

Description

When running a job with conditional flow, if steps in this job have the same name, the job doesn't behavior as expected (infinite loop on one step ...) because transitions in the job rely on step names. So in order to avoid such behavior, steps names in a flow job should be unique : spring batch should raise an exception or at least should log a warning.
Furthermore, in xml config, as step names are defined as ids of <step>, they must be unique : it is an xsd constraint.

Here an example :

// Job definition :
public Job flowjob() {
	return jobBuilderFactory.get("flowJob")
			.start(step1())
			.on("ODD").to(step2())
			.from(step1()).on("EVEN").to(step3()
			.from(step3()).next(step4())
			.end()
			.build();
}

// step1 definition with the name of step1 :
private Step step1() {
	return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
		@Override
		public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
			String exitStatus = (System.currentTimeMillis() % 2 == 0) ? "EVEN" : "ODD";
			contribution.setExitStatus(new ExitStatus(exitStatus));
			return RepeatStatus.FINISHED;
		}
	})
        .build();
}

//step2 definition with name of **step1** :
private Step step2() {
	return stepBuilderFactory.get("step1") /*Big mistake !*/
        .tasklet((contrib, chunk)->RepeatStatus.FINISHED)
        .build();
}

// and so on : step3 and step4 are defined as step2 with always the same name (step1)

When running the job, spring batch loops infinitely on step3.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how FlowJob transitions resolve and register step names in the Java configuration shown, then compare that with the XML id constraint described in the issue. Done means duplicate step names are detected with an exception or warning before they can cause an infinite loop; the issue does not name specific files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.