gpt4 book ai didi

spring - 如何在 Spring Batch 中创建动态步骤

转载 作者:行者123 更新时间:2023-12-04 12:43:37 24 4
gpt4 key购买 nike

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

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]) 这样的步骤并返回作业 ref 吗?

最佳答案

是的,您可以动态创建步骤并返回作业引用。以下是您如何在您的案例中执行此操作的示例:

@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 - 如何在 Spring Batch 中创建动态步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54853908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com