I recently upgraded an old application using Spring Batch 2.2.0 to 3.0.5
. I made the necessary changes to the DB tables and some minute code changes related to parameter APIs.
Now when I run the application it is working but if a step's exit status is FAILED the job's exist status is set to COMPLETED. This is causing issues as our application code treats this as a successful execution. I am getting around it by adding a code snippet in afterJob()
where I check the stepExecution
list and set the job exit status manually, but shouldn't the Spring Batch framework take care of the exit status?
Is there anything that I missed while upgrading?
Ref: http://docs.spring.io/spring-batch/reference/html/configureJob.html
The second and preferred way to stop execution is to set a stop flag in the step execution object. To set this stop flag, call the method StepExecution. setTerminateOnly() , which is equivalent to sending a stop message. As soon as Spring Batch gets control of the processing, it stops the job execution.
To fail the tasklet, just throw an exception from it.
public class StepExecution extends Entity. Batch domain object representation the execution of a step. Unlike JobExecution , there are additional properties related the processing of items such as commit count, etc.
When the job having error, is to be recovered by only re-running the target job, tasklet model can be chooseed to make recovery simple. In chunk model, it should be dealt by returning the processed data to the state before executing the job and by creating a job to process only the unprocessed data.
If you are using the Java based configuration, you could add .on(ExitStatus.FAILED.getExitCode()).fail()
to a step that should cause the job's exit status to be FAILED
:
jobBuilderFactory.get("jobName")
.start(firstStep())
.next(secondStep()).on(ExitStatus.FAILED.getExitCode()).fail()
.next(thirdStep())
.end()
.build()
You can use <fail>
transition element inside <job>
to instruct a Job to stop with a BatchStatus and ExisStatus of FAILED.
<step id="step2" parent="s2">
<fail on="FAILED" />
<next on="*" to="step3"/>
</step>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With