Spring-Batch:如何从StepListener返回自定义Job退出状态以决定下一步 [英] Spring-Batch: how do I return a custom Job exit STATUS from a StepListener to decide next step

查看:578
本文介绍了Spring-Batch:如何从StepListener返回自定义Job退出状态以决定下一步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The issue is this: I have a Spring Batch job with multiple step. Based on step one i have to decide the next steps. Can i set status in STEP1- passTasklet based on a job parameter so that i can set the Exit status to a custom status and define it in the job definition file to go to which next step.

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>

我理想地希望在步骤之间使用我自己的退出状态.我可以那样做吗?不会破坏任何OOTB流量吗?有效吗

i ideally want my own EXIT STATUS to be used between steps. Can i do that? will it not break any OOTB flow? is it valid to do

推荐答案

它们是执行此操作的几种方法.

They are several ways to do this.

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

You can use a StepExecutionListener and override the afterStep method:

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

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

Or use a JobExecutionDecider to choose the next step based on a result.

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

这篇关于Spring-Batch:如何从StepListener返回自定义Job退出状态以决定下一步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆