Repeating step x times in Spring Package

I am using Spring Batch 3.0.3, configured with annotation to create a batch job that repeats the step an undetermined number of times.

My first step will be to read into the memory a list of elements used during the repeating step. I would like the repeating steps to be repeated in this vacancy list.

How can I set up my work to perform the same step x times? I saw examples in the xml step, indicating the next step to run. I think I could point out two steps to each other in an infinite loop until the list is repeated. Will this work and is there a way to do this with annotations? Below is my main configuration file with some code that didn't work.

@ComponentScan(excludeFilters = @Filter(IgnoreDuringScan.class))
@EnableAutoConfiguration
@EnableBatchProcessing
@Loggable
public class BatchCrudConfiguration
{
    @Bean
    public Job batchCRUDJob(JobBuilderFactory jobBuilderFactory, Step[] processSheetSteps)
    {
        JobBuilder jobBuilder = jobBuilderFactory.get("batchCRUDJob").incrementer(new RunIdIncrementer());
        FlowBuilder<FlowJobBuilder> jobFlowBuilder = jobBuilder.flow(processSheetSteps[0]);
        for (int i = 1; i < processSheetSteps.length; i++)
        {
            jobFlowBuilder = jobFlowBuilder.next(processSheetSteps[i]);
        }
        return jobFlowBuilder.end().build();
    }

    @Bean
    public Step[] processSheetSteps(
            StepBuilderFactory stepBuilderFactory,
            RawDataReader[] readers,
            DelegatingWriter writer,
            DelegatingProcessor processor,
            @Value("${batchcrud.chunkSize}") int chunkSize)
    {
        int numberOfReaders = readers.length;
        Step[] steps = new Step[numberOfReaders];
        for (int i = 0; i < numberOfReaders; i++)
        {
            steps[i] = stepBuilderFactory.get("processSheet" + i + "Step").<RawData, DataItem>
                    chunk(chunkSize).reader(readers[i]).processor(processor).writer(writer).build();
        }
        return steps;
    }
+4
1

:

  • .
  • .
  • StepExecutionListener, , . , ExitStatus, . , ExitStatus, ( ).

:

StepExecutionListener

public class MyListener {

    @Autowired
    private List myItems;

    @AfterStep
    public ExitStatus afterStep(StepExecution stepExecution) {
        if(myItems.size() > 0) {
            return new ExitStatus("CONTINUE");
        }
        else {
            return new ExitStatus("FINISHED");
        }
    }
}

...
@Bean
public Step step1() {...}

@Bean
public MyListener listener() {..}

@Bean
public Step step2(MyListener listener) {
    return stepBuilder.get("step2")
                .tasklet(myTasklet()) // Replace this piece as needed
                .listener(listener).build();
}

@Bean
public Job job1(Step step1, Step step2) {
    return jobBuilder.get("job1")
                     .start(step1)
                     .next(step2).on("CONTINUE").to(step2).on("FINISHED").end()
                     .build();
}
...

. , ..

+8

All Articles