如何在Spring Batch中创建动态步骤 [英] How to create dynamic steps in Spring Batch

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

问题描述

我需要创建'N'个步骤,具体取决于从数据库接收到的'maxHierLevel值,并按顺序执行它们-

I need to create 'N' number of steps, depending on the 'maxHierLevel value received from the database and execute them sequentially -

int maxHierLevel = testService.getHighestLevel(); 

Step masterCalculationStep = stepBuilderFactory.get("CALCUL_STEP_1")
        .<Map<Long, List<CostCalculation>>, List<TempCostCalc>>chunk(1)
        .reader(reader)
        .processor(processor)
        .writer(writer)
        .build();

final Step[] stepsArray = new Step[maxHierLevel];

for (int i = 0; i < stepsArray.length; i++) {
    stepsArray [i] = stepBuilderFactory.get("processingRecordsInLevel_"+i)
            .partitioner("partitionningSlavStep_"+i , calculationPartioner(i))
            .step(masterCalculationStep)
            .listener(new StepResultListener())
            .taskExecutor(taskExecutor)
            .build();
}

return jobBuilderFactory.get("mainCalculationJob")
                .incrementer(new RunIdIncrementer())
                .flow(truncTableTaskletStep())
                .next(loadPlantList)
                .next(stepsArray[0]) 
                .next(stepsArray[1])
                .next(stepsArray[2])
                .end()
                .listener(listener)
                .build();

我们可以动态添加next(stepsArray [0])之类的步骤并返回作业参考吗?

can we dynamically adds steps like next(stepsArray[0]) and return job ref ?

推荐答案

是的,您可以动态创建步骤并返回作业参考.这是一个如何在您的情况下执行此操作的示例:

Yes you can create steps dynamically and return the job reference. Here is an example of how you can do it in your case:

@Bean
public Job job() {
    Step[] stepsArray = // create your steps array or pass it as a parameter
    SimpleJobBuilder jobBuilder = jobBuilderFactory.get("mainCalculationJob")
            .incrementer(new RunIdIncrementer())
            .start(truncTableTaskletStep());
    for (Step step : stepsArray) {
        jobBuilder.next(step);
    }
    return jobBuilder.build();
}

希望这会有所帮助.

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

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