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

时间:2014-11-28 05:32:38

标签: spring spring-batch

嗨,我有以下像xml执行作业

<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 :(得分:4)

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

<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;
}

答案 1 :(得分:0)

这可以通过指定自定义&#34; chunk-completion-policy&#34;来实现。在该步骤中计算失败次数。看看&#34; Stopping a Job Manually for Business Reasons&#34;和自定义块完成策略的this示例。希望这会有所帮助。

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