简体   繁体   中英

Spring batch - executing one step back OR Re-executing previously executed steps

I have a spring batch job with, lets say 5 steps ( step1 --> step2 --> step3 --> step4 --> step5 ) . I have configured a StepExectutionListener , which listens to 2 events

beforeStep() and afterStep() for all the steps.

My question is, if I am in currently beforeStep() method, and the step to be executed is step2 , can I make spring-batch execute step1 again? After executing step1 , the flow should continue to step2 , step3 and so on..

To clarify again, can I tell spring-batch to "start execution" from a previously executed step again?

While I probably wouldn't recommend this behavior, yes, it should be able to be accomplished. What you'll need to do is to configure step2 to have a transition to step1 with the correct exit code and step1 will need to be able to rerun.

Configuration

<step id="step1" next="step2">
    <tasklet ref="someTasklet" allow-start-if-complete="true"/>
</step>
<step id="step2">
    <tasklet ref="someOtherTasklet"/>
    <listeners>
        <listener ref="loopingListener"/>
    </listeners>
    <next on="BACK" to="step1"/>
</step>

Code

public class LoopingListener extends StepExecutionListenerSupport {

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        if(shouldLoop) {
            return new ExitStatus("BACK");
        } else {
            return stepExecution.getExitStatus();
        }
    }
}

You can achieve a particular ordering of the steps using the following approach.

<step id="step1" next="step3">
<tasklet>(...)</tasklet>
</step>

next parameter will define which will be executed next. However I am not sure whether you can step back in the same execution. But Spring allows you to restart a step if you use the

allow-start-if-complete

to true.

Also for building workflows, look at http://docs.spring.io/spring-batch/2.2.x/apidocs/org/springframework/batch/core/job/builder/FlowBuilder.SplitBuilder.html

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM