Spring 批量步骤失败

Spring Batch Step Failure

如果在多步骤批处理中出现步骤失败,步骤失败消息将显示在控制台上。 另外 BATCH_STATUSCOMPLETEDEXIT_STATUSFAILED.

Q.1 如何把EXIT_STATUS改成COMPLETED

Q.2 是否可以不在控制台中显示失败消息

示例代码

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

如果作业 someJob 的步骤 aStep 失败(aStep 已抛出一些 SQLException)。 然后someJob继续执行。但是在 cStep 成功完成后,someJobBATCH_STATUSCOMPLETEDEXIT_STATUS = FAILED。 另外BATCH_STATUSEXIT_STATUS其他步骤如下

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

Spring批处理中有两种状态。第一个是BatchStatus。此状态由一组预定义值组成,框架使用这些值来指示事物的状态。另一个是ExitStatusExitStatus 使开发人员能够提供特定于用例的状态消息。

在执行作业时,没有办法Spring批处理允许您覆盖BatchStatus。这是设计使然。但是,ExitStatus 允许在许多地方设置(通常是 StepExecutionListenerJobExecutionListener 等侦听器),以便可以解释框架的结果以满足您的使用案例.

如果您想在 ExitStatus 通常表示作业或步骤失败时指示成功,您可以实施适当的侦听器并相应地设置 ExitStatus

如果你参考 Spring Batch 的文档关于配置作业的页面(http://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html ,第 5.3.3 节,你应该可以看到 <end>

可以通过

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

这样做,当 step1 失败时,作业将以 COMPLETED 批处理和退出状态结束。

您可以在 xml 中添加以下代码:

如果想在步骤失败时使作业失败 -- 如果想在步骤失败时完成作业 --

如果您想在步骤失败时在日志中打印一条消息,请包括如下所示的异常处理:

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