【问题标题】:How to terminate Step within a Spring Batch Split Flow with a Decider如何使用决策者终止 Spring Batch Split Flow 中的步骤
【发布时间】:2014-02-14 14:30:08
【问题描述】:

我在 Spring Batch 中遇到了以下设计缺陷。

  1. 步骤必须具有 Next 属性,除非它是拆分流的最后一个步骤或最后一个步骤。
  2. Decider 块必须处理 Decider 返回的所有情况。

因此,在拆分流中,最终步骤不会有 Next 属性,如果有一个 Decider 保护它,那么它必须有一个 Next 属性。所以它不应该有那个属性,但它也需要它。第 22 条。

例子:

<!-- Process parallel steps -->
<split id="split01">
    <flow>
        <step id="step1" next="step02">
            <!-- Do something -->
        </step>
        <step id="step02">
            <!-- Do something else -->
        </step>
    </flow>
    <flow>
        <step id="step03">
            <!-- Do something -->
        </step>

        <!-- Only run under specific conditions -->
        <decision id="decideToRunStep04" decider="isStepNeededDecider" >
            <next on="RUN" to="step04"/>
            <!-- Other state is "SKIP" -->
        </decision>
        <step id="step04">
            <!-- Conditionally do something-->
        </step>
    </flow>
</split>

<step id="step05" >
    <!-- Some more stuff -->
</step>

这似乎是 Spring 家伙会想到的事情,所以很好奇实现这一点的正确、非 hack 方法是什么。谢谢。

【问题讨论】:

    标签: spring-batch


    【解决方案1】:

    鉴于没有任何人对此作出回答,我将提供我正在使用的破解方法。它不漂亮,但 Spring 也不漂亮。

    创建一个 No Op Tasklet 以在 No Op 步骤中使用。

    public class NoopTasklet implements Tasklet {
        @Override
        public RepeatStatus execute(final StepContribution contribution,
                final ChunkContext chunkContext) throws Exception {
            return RepeatStatus.FINISHED;
        }
    }
    

    将 NOOP tasklet 添加到原始示例中的决策块

    <!-- Does nothing -->
    <bean id="noopTasklet" class="com.foo.NoopTasklet" />
    
    <!-- From example in question
    <decision id="decideToRunStep04" decider="isStepNeededDecider" >
        <next on="RUN" to="step04"/>
        <next on="SKIP" to="noop01"/>
    </decision>
    <step id="step04">
        <!-- Conditionally do something-->
    </step>
    <step id="noop01">
        <!-- Does nothing in the SKIP case
        <tasklet ref="noopTasklet" />
    </step>
    

    【讨论】:

    【解决方案2】:

    春天是镇上最漂亮的代码。 也就是说:

    <step id="step1" parent="s1">
        <end on="FAILED" />
        <next on="COMPLETED WITH SKIPS" to="errorPrint1" />
        <next on="*" to="step2" />
    </step>
    

    正如http://docs.spring.io/spring-batch/reference/html/configureStep.html 所记录的那样。

    【讨论】:

    • 查看所述问题。这并不能解决问题。
    • 你说得对,我试图忽略“
    • 这很干净,它适用于我的实现。我将它与我自己在这里看到的决策者实现结合使用:stackoverflow.com/questions/15411731/…
    【解决方案3】:

    在 XML 中

    <batch:decision id="customerDecision" decider="customerDecider">
                <batch:next on="FILE_FAILURE" to="fileFailureStep" />
                <batch:next on="FILE_GENERATION" to="loadData" /> 
    </batch:decision>
    

    在 customerDecider 类中

    public class CustomerDecider implements JobExecutionDecider {    
    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecutionStatus) {
    If(x)
        return new FlowExecutionStatus("FILE_FAILURE") ;
    else
        return new FlowExecutionStatus("FILE_GENERATION") ;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-20
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多