Sanjib Kumar Koley
Sanjib Kumar Koley

Reputation: 81

Spring Batch Step Failure

In case of step failure in a multi-step batch , the step fail message is shown on console. Also the BATCH_STATUS is COMPLETED and EXIT_STATUS is FAILED.

Q.1 How to change the EXIT_STATUS to COMPLETED?

Q.2 Is it anyway possible not to show failure message in console

Sample Code

 <job id="someJob" xmlns="http://www.springframework.org/schema/batch"
     restartable="true" parent="baseJob">
    <step id="aStep">
        <job ref="aJob" />
        <next on="*" to="bStep"/>
    </step>
    <step id="bStep" parent="aStep" next="cStep">
        <job ref="bJob"/>
    </step>
    <step id="cStep" parent="bStep">
        <job ref="cJob"/>
    </step>
</job>

 <job id="aJob" xmlns="http://www.springframework.org/schema/batch"
         restartable="true" parent="baseJob">
    <step id="aJobStep">
        <tasklet ref="aJobTasklet" />
    </step>
</job>

<job id="bJob" xmlns="http://www.springframework.org/schema/batch"
         restartable="true" parent="baseJob">
    <step id="bJobStep">
        <tasklet ref="bJobTasklet" />
    </step>
</job>

<job id="cJob" xmlns="http://www.springframework.org/schema/batch"
         restartable="true" parent="baseJob">
    <step id="cJobStep">
        <tasklet ref="cJobTasklet" />
    </step>
</job>

if Step aStep fails for job someJob (aStep has thrown some SQLException). Then someJob is continued to execute. But after successful completion of cStep, BATCH_STATUS for someJob is COMPLETED but EXIT_STATUS = FAILED. Also BATCH_STATUS and EXIT_STATUS of other steps are as follows.

Step Name BATCH_STATUS  EXIT_STATUS
aStep     FAILED        FAILED
bStep     COMPLETED     COMPLETED
cStep     COMPLETED     COMPLETED

Upvotes: 3

Views: 27018

Answers (3)

A Pali
A Pali

Reputation: 1

You can add the below code in the xml:

If want to fail the job upon step failure -- <batch:end exit-code="FAILED" on="FAILED" /> If want to complete the job upon step failure -- <batch:end exit-code="COMPLETED" on="FAILED" />

Include exception handling like below if you want to print a message in logs upon step failure:

            <batch:skippable-exception-classes>
                <batch:include class="java.lang.Exception" />
            </batch:skippable-exception-classes>
            <batch:listeners>
                <batch:listener>
                    <bean  class="java.lang.Exception" scope = "step">
                    </bean>
                </batch:listener>
            </batch:listeners> 

Upvotes: 0

Adrian Shum
Adrian Shum

Reputation: 40036

If you refer to Spring Batch's document on the page about configuring the job (http://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html , section 5.3.3, you should be able to see the use of <end>

It can be done by

<job ....>
    <step id="step1"...>
        ....
        <end on="FAILED" />
        <next on="*" to="step2" />
    </step>
    <step id="step2" ....>
        ....
    </step>
</job>

By doing so, when step1 failed, the job will end with COMPLETED Batch and Exit status.

Upvotes: 1

Michael Minella
Michael Minella

Reputation: 21463

There are two statuses in Spring Batch. The first is the BatchStatus. This status consists of a set of pre-defined values that are used by the framework to indicate the state of things. The other is the ExitStatus. The ExitStatus provides the ability for a developer to provide use case specific status messages.

In the execution of a job, there is no way Spring Batch allows you to override the BatchStatus. This is by design. However, the ExitStatus is allowed to be set in a number of places (typically listeners like the StepExecutionListener and the JobExecutionListener) so that the results from the framework can be interpreted to meet your use case.

If you want to indicate success when the ExitStatus typically says that the job or step has failed, you would implement the appropriate listener and set the ExitStatus accordingly.

Upvotes: 6

Related Questions