【问题标题】:Spring Batch Step FailureSpring批处理步骤失败
【发布时间】:2015-01-06 06:08:46
【问题描述】:

如果多步骤批处理中的步骤失败,控制台上会显示步骤失败消息。 BATCH_STATUS 也是 COMPLETEDEXIT_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>

如果步骤aStep 对作业someJob 失败(aStep 抛出了一些SQLException)。 然后someJob继续执行。但是在成功完成cStep之后,BATCH_STATUS对于someJobCOMPLETED但是EXIT_STATUS=FAILED。 还有BATCH_STATUSEXIT_STATUS的其他步骤如下。

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

【问题讨论】:

  • 您的意思是要忽略步骤中发生的错误吗?我认为你可以通过使用 flow 来做到这一点

标签: spring-batch


【解决方案1】:

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

在执行作业时,Spring Batch 不允许您覆盖BatchStatus。这是设计使然。但是,ExitStatus 允许在多个位置设置(通常是像 StepExecutionListenerJobExecutionListener 这样的侦听器),以便可以解释框架的结果以满足您的用例。

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

【讨论】:

  • 从“aStep”的 StepListener 的 afterStep 返回 ExitStatus.COMPLETED 不起作用
  • 批处理状态仍为 COMPLETED 且退出状态为 FAILED
  • @Override public ExitStatus afterStep(StepExecution stepExecution) { //Step Listener 在多步批处理的每一步中被调用 //If 条件仅在所需的步骤清除异常列表 if (StringUtils.equals(stepExecution .getStepName(), "aStep")) { stepExecution.getFailureExceptions().clear(); stepExecution.getJobExecution().getAllFailureExceptions().clear(); stepExecution.setExitStatus(ExitStatus.COMPLETED);返回 ExitStatus.COMPLETED; } 返回空值;现在批处理状态 = COMPLETED 和退出状态 = COMPLETED 即使“aStep”失败。
【解决方案2】:

如果你参考Spring Batch的页面上关于配置job的文档(http://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html, section 5.3.3,应该可以看到&lt;end&gt;的用法

可以通过

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

这样做,当 step1 失败时,作业将以COMPLETED Batch and Exit 状态结束。

【讨论】:

  • 如果 step1 失败 step2 由于 元素而没有执行
  • 会在您的cStep 帮助中添加&lt;end on="*" /&gt; 吗?
【解决方案3】:

您可以在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> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2020-09-10
    • 2021-03-01
    相关资源
    最近更新 更多