【问题标题】:Spring-Batch: how do I return a custom Job exit STATUS from a StepListener to decide next stepSpring-Batch:如何从 StepListener 返回自定义作业退出状态以决定下一步
【发布时间】:2013-03-02 22:43:56
【问题描述】:

问题是这样的:我有一个包含多个步骤的 Spring Batch 作业。基于第一步,我必须决定下一步。我可以根据作业参数在 STEP1- passTasklet 中设置状态,以便我可以将退出状态设置为自定义状态并在作业定义文件中定义它以转到下一步。

Example
<job id="conditionalStepLogicJob">
<step id="step1">
<tasklet ref="passTasklet"/>
<next on="BABY" to="step2a"/>
<stop on="KID" to="step2b"/>
<next on="*" to="step3"/>
</step>
<step id="step2b">
<tasklet ref="kidTasklet"/>
</step>
<step id="step2a">
<tasklet ref="babyTasklet"/>
</step>
<step id="step3">
<tasklet ref="babykidTasklet"/>
</step>
</job>

理想情况下,我希望在步骤之间使用我自己的 EXIT STATUS。我可以这样做吗?它不会破坏任何 OOTB 流程吗?这样做是否有效

【问题讨论】:

    标签: spring spring-batch


    【解决方案1】:

    有几种方法可以做到这一点。

    您可以使用StepExecutionListener 并覆盖afterStep 方法:

    @AfterStep
    public ExitStatus afterStep(){
        //Test condition
        return new ExistStatus("CUSTOM EXIT STATUS");
    }
    

    或使用JobExecutionDecider 根据结果选择下一步。

    public class CustomDecider implements JobExecutionDecider  {
    
        public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
            if (/* your conditon */) {
                return new FlowExecutionStatus("OK");
            }
            return new FlowExecutionStatus("OTHER CODE HERE");
        }
    
    }
    

    Xml 配置:

        <decision id="decider" decider="decider">
            <next on="OK" to="step1" />
            <next on="OHTER CODE HERE" to="step2" />
        </decision>
    
    <bean id="decider" class="com.xxx.CustomDecider"/>
    

    【讨论】:

    • 如果没有更多步骤,也可以使用
    • 如果您在决策标签中添加“*”作为 else 语句,它也应该可以工作。有没有人不会,它会被解释为其他?如果决策者可以返回 3 个不同的值,并且在批处理作业中您只能解释两种方式
    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2011-08-29
    • 2015-04-30
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多