首页 文章

如何控制 spring 批次中的重复和下一步执行

提问于
浏览
2

嗨我下面像xml一样执行Job

<batch:job id="Job1" restartable="false" xmlns="http://www.springframework.org/schema/batch">
        <step id="step1" next="step2">
            <tasklet ref="automate" />
        </step>
        <step id="step2">
            <tasklet ref="drive"  />
            <next on="COMPLETED" to="step3"></next>
        </step>
        <step id="step3">
            <tasklet ref="generate_file"  />
        </step>
    </batch:job>

为此,我编写了一个tasklet来执行脚本 . 现在我希望如果脚本执行失败三次,那么下一步就不会执行 . 但是从Tasklet我只能返回Finished,它将流程移动到下一步并且可以继续进行,继续流程 . 我该怎么做呢

2 回答

  • 0

    你可以编写自己的决策来决定下一步或结束工作 . 如果你能够处理失败,你也可以处理工作流程

    <decision id="validationDecision" decider="validationDecider">
            <next on="FAILED" to="abcStep" />
            <next on="COMPLETE" to="xyzstep" />
        </decision>
    

    配置是

    <bean id="validationDecider" class="com.xyz.StepFlowController" />
    

    上课是

    public class StepFlowController implements JobExecutionDecider{
    
    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        FlowExecutionStatus status = null;
        try {
            if (failure) {
    status = new FlowExecutionStatus("FAILED");
    
            }else {
                status = new FlowExecutionStatus("COMPLETE");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return status;
    }
    
  • 4

    这可以通过在该步骤中指定自定义"chunk-completion-policy"并计算失败次数来实现 . 查看自定义块完成策略的“Stopping a Job Manually for Business Reasons”和this示例 . 希望这可以帮助 .

    编辑:您可以将步骤执行上下文中的失败次数放入步骤逻辑中,然后在完成策略类中检索它:stepExecution.getJobExecution() . getExecutionContext() . put(“ERROR_COUNT”,noOfErrors);

相关问题