2012-09-17 56 views
4

这是我需要做的测试目的。我需要一种方法来根据某些预设条件(例如已处理项目的数量)在我想要的时候使Spring Batch作业失败。但是,迄今为止,我还没有找到类似的东西。如何手动失败Spring批处理作业?

我发现Spring Batch的有这样的事情 -

<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"> 

但是我期待的是像这个 -

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

如何做到这一点?

更新:作业可失败了一步之外也是如此。

+0

@maxhax我的意思是在beforeStep或afterStep方法中使用监听器。在afterStep/beforeStep方法中的 – CodeBlue

+0

它应该足以返回ExitStatus.FAILED。要么? 否则只是抛出一个execption在微进程,迫使你的脚步失败。 – maxhax

+0

@maxhax是的,这就是我现在正在做的。 – CodeBlue

回答

3
public void myJobFailingMethod() 
{ 
    if(conditionsMatch()) 
    { 
     throw new CustomJobFailingException(); // create this exception class. 
               // It will fail the job. 
    } 
} 
相关问题