简体   繁体   中英

Creating a iterating flow steps in Spring Batch

I'm doing a flow step who needs to repeat the same step in diferent moment. It's not repeating same step until we have RepeatStatus.FINISHED, but after some others steps go to a previous step. We have done a simplificated model to try it but it also doesn't work.

Job.xml:

<job id="job1" xmlns="http://www.springframework.org/schema/batch"> 
    <step id="job1Step1" next="decision1"> 
        <tasklet ref="tasklet1" />  
    </step> 

    <decision id="decision1" decider="decider1">
        <next on="1" to="job1Step1"/>
        <next on="2" to="job1Step2"/>
        <next on="3" to="job1Step3"/>
    </decision>

    <step id="job1Step2" next="decision2"> 
        <tasklet ref="tasklet2" />  
    </step>

    <decision id="decision2" decider="decider2">
        <next on="1" to="job1Step1"/>
        <next on="2" to="job1Step2"/>
        <next on="3" to="job1Step3"/>
    </decision>

    <step id="job1Step3" next="job1Step1"> 
        <tasklet ref="tasklet3" />  
    </step>
</job>

Beans:

<bean id="decider1" class="Decider1"/>
<bean id="decider2" class="Decider2"/>

<bean id="tasklet1" class="Tasklet1"/>
<bean id="tasklet2" class="Tasklet2"/>
<bean id="tasklet3" class="Tasklet3"/>

Then, there are models of java classes:

Tasklet class model:

public class TaskletN implements Tasklet {

    protected static Log log = LogFactory.getLog(TaskletN.class);

    @Override
    public RepeatStatus execute(StepContribution stepCont, ChunkContext chunkContext) throws Exception {
        log.info("Passo per TASKLET_N");
        return null;
    }

}

Decider class model:

public class DeciderM implements JobExecutionDecider {

protected static Log log = LogFactory.getLog(DeciderM.class);

    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        String prova = "M+1";
        log.info("Estic a DECIDER_M i vaig al STEP: " + prova);
        return new FlowExecutionStatus(prova);
    }

}

And, that's the problem. First time it's alright but second and other times it goes to deciders directly intead of steps.

Passo per TASKLET_1
Estic a DECIDER_1 i vaig al STEP: 2
Passo per TASKLET_2
Estic a DECIDER_2 i vaig al STEP: 3
Passo per TASKLET_3
Estic a DECIDER_1 i vaig al STEP: 2
Estic a DECIDER_2 i vaig al STEP: 3
Estic a DECIDER_1 i vaig al STEP: 2
Estic a DECIDER_2 i vaig al STEP: 3
Estic a DECIDER_1 i vaig al STEP: 2
... (bug)

There is a similar problem described in

How to run a step in a loop in Spring Batch : Updated

Have a look at my comment under my answer to that question:

This will not work. SpringBatch is not intended to execute a specific step several times during a single job launch. The framework keeps several internal states of the step and its reader and writer. You would need to hack into the instantiated job structure during runtime and somehow reset the states of the step and the readers and writers. But that's so ugly and a dirty hack; definitely not something you wanna do.

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