在Spring Batch中重复步骤x次 [英] Repeating a Step x times in Spring Batch

查看:182
本文介绍了在Spring Batch中重复步骤x次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用配置了批注的Spring Batch 3.0.3创建一个批处理作业,该操作重复执行一次未确定的次数.

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 list of items used during the repeating step. I'd like the repeating steps to iterate through this job-scoped list.

如何配置作业以执行相同的步骤x次?我已经在xml的示例中看到了指定下一步运行的示例.我想我可以无限循环地指向两个步骤,直到列表被迭代为止.这项工作是否可行,是否可以通过注释进行?以下是我的主要配置文件,其中包含一些无法使用的代码.

How can I configure my job to run the same step x times? I've seen examples in xml of a step specifying the next step to run. I'm thinking I could point two steps to each other in an infinite loop until the list has been iterated. Would this work and is there a way to do it with annotations? Below is my main configuration file with some code that wasn't working.

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

推荐答案

我通过以下方法实现此目的:

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

  1. 第一步加载列表.
  2. 第二步处理列表中的项目.
  3. 第二步有一个StepExecutionListener,用于评估列表中是否还有其他项目要处理.如果是这样,它将返回映射到同一步骤的ExitStatus.如果不是,它将返回一个ExitStatus,该映射映射到结束作业或继续该作业(基于其余流程).
  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).

例如:

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

请注意,我尚未测试此代码,因此可能会有错别字等.

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

这篇关于在Spring Batch中重复步骤x次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆