4

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

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

PAA
  • 1
  • 46
  • 174
  • 282

1 Answers1

6

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

Hope this helps.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Do you know how to make these steps context aware. So that they could for example use @BeforeStep annotation? – najczuk Jul 09 '20 at 14:02
  • @MahmoudBenHassine thanks for proving solution for adding steps dynamically to batch job, I have additional use case run them parallel any input would appreciated? – ssan Sep 20 '20 at 07:56