Repeating a Step x times in Spring Batch

前端 未结 1 1652
时光取名叫无心
时光取名叫无心 2021-02-09 05:43

I\'m using Spring Batch 3.0.3 configured with annotation to create a batch job that repeats a step an undetermined number of times.

My first step will read into memory a

1条回答
  •  长发绾君心
    2021-02-09 06:07

    The way I'd approach this is via the following:

    1. First step loads the list.
    2. Second step processes an item in the list.
    3. The second step has a StepExecutionListener that evaluates if there are more items in the list to process. If so, it returns an ExitStatus that maps to the same step. If not, it returns an ExitStatus that maps to end the job or continue the job (based on the rest of the flow).

    For example:

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

    Job Configuration

    ...
    @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();
    }
    ...
    

    Note I have not tested this code so there may be typos, etc.

    0 讨论(0)
提交回复
热议问题