Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring batch job exit status for failed step

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

like image 824
Jit B Avatar asked Sep 12 '15 08:09

Jit B


People also ask

How do you stop a step in Spring Batch?

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.

How do I fail a Tasklet in Spring Batch?

To fail the tasklet, just throw an exception from it.

What is StepExecution in Spring Batch?

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.

What is the difference between Tasklet and chunk in Spring Batch?

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.


2 Answers

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()
like image 163
badjr Avatar answered Sep 29 '22 01:09

badjr


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>
like image 45
Ravi Avatar answered Sep 29 '22 01:09

Ravi