Jit B
Jit B

Reputation: 1246

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

Upvotes: 8

Views: 8591

Answers (3)

Prashant Shukla
Prashant Shukla

Reputation: 1

You can use Decider on that job and that decider call a class like this:

public class JobDecider implements JobExecutionDecider  {

  /* @Autowired
    private JobOperator jobOperator;*/

    @Override
    public FlowExecutionStatus decide(JobExecution arg0, StepExecution stepExecution) {

    JobParameters jp = arg0.getJobParameters();

    if(!stepExecution.getExitStatus().getExitCode().equals("COMPLETED"))
    {
        return FlowExecutionStatus.FAILED;
    }
    else 
    {
        //System.out.println("*******Job Decider ******* With Passed State ");

        return FlowExecutionStatus.COMPLETED;
    }

    }

}

Upvotes: 0

badjr
badjr

Reputation: 2276

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()

Upvotes: 4

Ravi
Ravi

Reputation: 21

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>

Upvotes: 2

Related Questions