Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually fail a Spring Batch job?

This is something I need to do for testing purposes. I need a way to fail a Spring Batch job whenever I want based on certain preset conditions (such as number of items processed). However, I haven't been able to find anything like this so far.

I have found that Spring Batch has something like this -

<step id="step1" parent="s1" next="step2">

<step id="step2" parent="s2">
    <fail on="FAILED" exit-code="EARLY TERMINATION"/>
    <next on="*" to="step3"/>
</step>

<step id="step3" parent="s3">

But what I am looking for is something like this-

public void myJobFailingMethod()
 {
    if(conditionsMatch())
     {
        // fail the job 
     }  
 }

How to do this?

Update: The Job can be failed outside a step as well.

like image 390
CodeBlue Avatar asked Oct 07 '22 14:10

CodeBlue


1 Answers

public void myJobFailingMethod()
{
    if(conditionsMatch())
    {
        throw new CustomJobFailingException(); // create this exception class.
                                               // It will fail the job.
    }  
}
like image 70
CodeBlue Avatar answered Oct 10 '22 04:10

CodeBlue