Spring-Batch:如何从StepListener返回自定义Job退出STATUS以决定下一步

tec*_*uva 5 spring spring-batch

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

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>
Run Code Online (Sandbox Code Playgroud)

我理想地希望在步骤之间使用我自己的EXIT STATUS.我能这样做吗?它不会破坏任何OOTB流量吗?这样做有效吗?

Ben*_*chi 10

他们有几种方法可以做到这一点.

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

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

或者使用a 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");
    }

}
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)