title: Spring batch execute dynamically generated steps in a tasklet
body: <p>I have a spring batch job that does the following...</p>
  
  <p>Step 1. Creates a list of objects that need to be processed</p>
  
  <p>Step 2. Creates a list of steps depending on how many items are in the list of objects created in step 1.</p>
  
  <p>Step 3. Tries to executes the steps from the list of steps created in step 2. </p>
  
  <p>The executing x steps is done below in executeDynamicStepsTasklet(). While the code runs without any errors it does not seem to be doing anything. Does what I have in that method look correct?</p>
  
  <p>thanks</p>
  
  <p>/*
   *
   */ </p>
  
  <pre><code>@Configuration
  public class ExportMasterListCsvJobConfig {
  
  public static final String JOB_NAME = "exportMasterListCsv";
  @Autowired
  public JobBuilderFactory jobBuilderFactory;
  
  @Autowired
  public StepBuilderFactory stepBuilderFactory;
  
  @Value("${exportMasterListCsv.generateMasterListRows.chunkSize}") 
  public int chunkSize;
  
  @Value("${exportMasterListCsv.generateMasterListRows.masterListSql}") 
  public String masterListSql;
  
  @Autowired
  public DataSource onlineStagingDb;
  
  @Value("${out.dir}") 
  public String outDir;
  
  @Value("${exportMasterListCsv.generatePromoStartDateEndDateGroupings.promoStartDateEndDateSql}") 
  private String promoStartDateEndDateSql;
  
  
  private List&lt;DivisionIdPromoCompStartDtEndDtGrouping&gt; divisionIdPromoCompStartDtEndDtGrouping;
  
  private List&lt;Step&gt; dynamicSteps = Collections.synchronizedList(new ArrayList&lt;Step&gt;()) ;
  
  
  @Bean
  public Job exportMasterListCsvJob(
          @Qualifier("createJobDatesStep") Step createJobDatesStep,
          @Qualifier("createDynamicStepsStep") Step createDynamicStepsStep,
          @Qualifier("executeDynamicStepsStep") Step executeDynamicStepsStep) {
  
      return jobBuilderFactory.get(JOB_NAME)
              .flow(createJobDatesStep)
              .next(createDynamicStepsStep)
              .next(executeDynamicStepsStep)
              .end().build();
  }   
  
  
  @Bean
  public Step executeDynamicStepsStep(
          @Qualifier("executeDynamicStepsTasklet")  Tasklet executeDynamicStepsTasklet) {
  
      return  stepBuilderFactory
                  .get("executeDynamicStepsStep")
                  .tasklet(executeDynamicStepsTasklet)
                  .build();               
  }
  
  @Bean
  public Tasklet executeDynamicStepsTasklet() {
  
      return new Tasklet() {
  
          @Override
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
              FlowStep flowStep = new FlowStep(createParallelFlow());
              SimpleJobBuilder jobBuilder = jobBuilderFactory.get("myNewJob").start(flowStep);
              return RepeatStatus.FINISHED;
          }
      };
  }
  
  public Flow createParallelFlow() {
      SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
      taskExecutor.setConcurrencyLimit(1); 
  
      List&lt;Flow&gt; flows = dynamicSteps.stream()
              .map(step -&gt; new FlowBuilder&lt;Flow&gt;("flow_" + step.getName()).start(step).build())
              .collect(Collectors.toList());
  
      return new FlowBuilder&lt;SimpleFlow&gt;("parallelStepsFlow")
              .split(taskExecutor)
              .add(flows.toArray(new Flow[flows.size()]))
              .build();
  }
  
  @Bean
  
  public Step createDynamicStepsStep(
          @Qualifier("createDynamicStepsTasklet")  Tasklet createDynamicStepsTasklet) {
  
      return  stepBuilderFactory
                  .get("createDynamicStepsStep")
                  .tasklet(createDynamicStepsTasklet)
                  .build();               
  }   
  
  @Bean
  @JobScope
  public Tasklet createDynamicStepsTasklet() {
  
      return new Tasklet() {
  
          @Override
          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
              for (DivisionIdPromoCompStartDtEndDtGrouping grp: divisionIdPromoCompStartDtEndDtGrouping){
  
                  System.err.println("grp: " + grp);
  
                  String stepName = "stp_" + grp;
  
                  String fileName = grp + FlatFileConstants.EXTENSION_CSV;
  
                  Step dynamicStep = 
                          stepBuilderFactory.get(stepName)
                          .&lt;MasterList,MasterList&gt; chunk(10)
                          .reader(queryStagingDbReader(
                                  grp.getDivisionId(), 
                                  grp.getRpmPromoCompDetailStartDate(), 
                                  grp.getRpmPromoCompDetailEndDate()))
                          .writer(masterListFileWriter(fileName))                                
                          .build(); 
  
                  dynamicSteps.add(dynamicStep);
  
              } 
              System.err.println("createDynamicStepsTasklet dynamicSteps: " + dynamicSteps);
              return RepeatStatus.FINISHED;
          }
      };
  }
  
  
  public FlatFileItemWriter&lt;MasterList&gt; masterListFileWriter(String fileName) {
      FlatFileItemWriter&lt;MasterList&gt; writer = new FlatFileItemWriter&lt;&gt;();
      writer.setResource(new FileSystemResource(new File(outDir, fileName )));
      writer.setHeaderCallback(masterListFlatFileHeaderCallback());
      writer.setLineAggregator(masterListFormatterLineAggregator());
      return writer;
  }
  </code></pre>
  
  <p>So now I have a list of dynamic steps that need to be executed and I believe that they are in StepScope. Can someone advise me on how to execute them</p>