Spring Batch -- Data not getting promoted to future Step

26 Views Asked by At

a data i'm putting in the JobExecution map is not getting promoted to a future Step.

This is the XML file:

<batch:job id="job1">
    <step id="step1">
        <batch:tasklet ref="tasklet1">
        </batch:tasklet>
        <batch:next on="SUCCESS" to="tasklet2"/>
        <batch:end on="STOP"/>
        <batch:listeners>
            <listener ref="promotionListener1"/>
        </batch:listeners>
    </step>

    <step id="step2">
        <batch:tasklet ref="tasklet2">
        </batch:tasklet>
        <batch:listeners>
            <listener ref="promotionListener2"/>
        </batch:listeners>
    </step>
</job>

<bean id="tasklet1" class="....Tasklet1" scope="step">
</bean>
<bean id="tasklet2" class="....Tasklet2" scope="step"/>

<bean id="promotionListener1" class="org.spr....ExecutionContextPromotionListener">
    <property name="keys" value="key1"/>
</bean>
<bean id="promotionListener2" class="org.spr....ExecutionContextPromotionListener">
    <property name="keys" value="key2"/>
</bean>

This is the Java files for Tasklet1 & Tasklet2.

public class Tasklet1 implements Tasklet, InitializingBean, StepExecutionListener {

  private StepExecution stepExecution;

  @Override
  public void beforeStep(StepExecution stepExecution) {
    this.stepExecution = stepExecution;
  }

  @Override
  public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
    ...
    List<Integer> list = new ArrayList<>();
    list = dataMapper.retrieveData();
    ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    stepContext.put("key1", list);
    logger.info("list put: " + stepContext.get("key1"); // list put: [132, 943, 4]
    ...
  }
}
public class Tasklet2 implements Tasklet, InitializingBean, StepExecutionListener {

  List<Integer> list;

  private StepExecution stepExecution;

  @Override
  public void beforeStep(StepExecution stepExecution) {
    JobExecution jobExecution = stepExecution.getJobExecution();
    ExecutionContext jobContext = jobExecution.getExecutionContext();
    this.list = (ArrayList<Integer>) jobContext.get("key1");
    logger.info("list retrieved: " + stepContext.get("key1"); // list retrieved: null
    this.stepExecution = stepExecution;
  }

  @Override
  public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
    ...
    Integer packetNumber = dataMapper.retrievePacketNum();
    ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    stepContext.put("key2", packetNumber);
    ...
  }
}

i am using 2 different listeners, to promote 2 different data, each mapped to key1 & key2.

it's not working because in the log statement in Tasklet2, i can see the object retrieved from the key1 key is null

Additional Note:

  • tasklet1 has the batch:next configuration, not sure if that's causing the ExecutionContext to not get promoted

anyone able to help me troubleshoot?


I was able to retrieve the data in the next Step by using the following code instead, reference was this SO post.

@BeforeStep
    public void retrieveInterStepData(StepExecution stepExecution) {
        JobExecution jobExecution = stepExecution.getJobExecution();
        Collection<StepExecution> stepExecutions = jobExecution.getStepExecutions();
        for (StepExecution steps : stepExecutions) {
            ExecutionContext executionContext = steps.getExecutionContext();
            if (executionContext.containsKey("keys")) {
                this.nationalityMap = (Map<String, String>) executionContext.get("keys");
            }
        }
    }
0

There are 0 best solutions below