spring-batch icon indicating copy to clipboard operation
spring-batch copied to clipboard

Conditional Flow does not work as stated in official documentation

Open m4rcc opened this issue 3 years ago • 4 comments
trafficstars

The conditional flow does not work as stated in the official documentation (https://docs.spring.io/spring-batch/docs/current/reference/html/step.html#controllingStepFlow)

Tried with: Spring Boot: 2.6.7 >> <spring-batch.version>4.3.5</spring-batch.version> >> Java8 Spring Boot: 2.6.9 >> <spring-batch.version>4.3.6</spring-batch.version> >> Java8

@Bean
public Job job() {
	return this.jobBuilderFactory.get("job")
				.start(stepA())
				.on("*").to(stepB())
				.from(stepA()).on("FAILED").to(stepC())
				.end()
				.build();
}

If I use the above example, then:

stepA() is completed successfully then it runs ok to stepB() >> as expected stepA() fails then step stepB() is executed >> expected stepC() to be executed but instead it was again stepB()

If I reverse the order:

@Bean
public Job job() {
	return this.jobBuilderFactory.get("job")
				.start(stepA())
                                 .on("FAILED").to(stepC())
				.from(stepA()).on("*").to(stepB())
				.end()
				.build();
}

Then I get the following results:

stepA() is completed successfully then it throws Flow execution ended unexpectedly ... Next state not found in flow=... for state=job.step0 with exit status=COMPLETED stepA() fails then step stepC() is executed >> as expected

The above code was to simplify the explanation, I have attached the example I used. I simulated FAILURE in the first step by throwing an error from FirstItemReader.

Could this be a regression of https://github.com/spring-projects/spring-batch/issues/3638

Also, what is the difference between, is the latter a valid Flow syntax?:

@Bean
public Job job() {
	return this.jobBuilderFactory.get("job")
				.start(stepA()).on("*").to(stepB())
				.from(stepA()).on("FAILED").to(stepC())
				.end()
				.build();
}

and:

@Bean
public Job job() {
	return this.jobBuilderFactory.get("job")
				.start(stepA()).on("*").to(stepB()).end();
				.start(stepA()).on("FAILED").to(stepC()).end()
				.build();
}

Thank you

spring-batch-demo.zip

m4rcc avatar Jul 16 '22 13:07 m4rcc

I've noticed similar behavior regarding unexpected transitions. I've debugged it down to the change in FlowBuilder.java between spring-batch-core 4.2.4 and 4.2.5. In 4.2.4 the step name is used to create the name for the state. This creates consistent transitions. in 4.2.5 a counter is used to create the name. Due to an incorrect increment an invalid transition is created.

For a local fix I've added FlowBuilder version 4.2.4 in our code and use that as the flowbuilder.

Below is an image of the same transitions generated by the two versions. The left one is correct. The right one fails with a "flow execution ended unexpectedly".

transitions_424_425

MartijnKruissen avatar Aug 10 '22 11:08 MartijnKruissen

Hi, Is there any plan for this bug to be fixed?

cmarcu55 avatar Oct 04 '22 11:10 cmarcu55

HI, We also face the same issue. Can you please look in to the same

suchins3 avatar Mar 16 '23 10:03 suchins3

Thank you for opening this issue. We do not exclude that this could be a bug in Spring Batch. However, we would like to validate that with a minimal complete verifiable example.

Could you please take some time to create a minimal example that reproduces the problem? To help you in reporting your issue, we have prepared a project template that you can use as a starting point. Please check the Issue Reporting Guidelines for more details about this.

Thank you for your collaboration.

fmbenhassine avatar Apr 05 '23 08:04 fmbenhassine