2

How to pass data between steps in Spring Batch job using Java configuration and not XML configuration ?

Loic Mouchard
  • 1,121
  • 7
  • 22
Ghassen
  • 591
  • 1
  • 15
  • 33
  • so do you have configuration in xml & simply want a corresponding Java config? Elaborate more as what kind of data you wish to pass on. – Sabir Khan Aug 30 '17 at 03:24
  • 1
    Possible duplicate of [How can we share data between the different steps of a Job in Spring Batch?](https://stackoverflow.com/questions/2292667/how-can-we-share-data-between-the-different-steps-of-a-job-in-spring-batch). This answer contains a javaconfig example, but convert XML to javaconfig is not so hard so you can easly try yourself. Enjoy. – Luca Basso Ricci Aug 30 '17 at 08:02
  • Actually I don't have the XML config, I started all my project with Java config only – Ghassen Aug 30 '17 at 08:30

1 Answers1

4

Finally I found a solution to share data between steps without any XML config : First thing is to make the Tasklets classes implements StepExecutionListener and for the tasklet that's sending data put :

@Override
public void beforeStep(StepExecution stepExecution) {}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    stepExecution.getJobExecution().getExecutionContext().putString("test_key","test_value");
    return null;
}

and the second tasklet that must get data put :

@Override
public void beforeStep(StepExecution stepExecution) {
    test = stepExecution.getJobExecution().getExecutionContext().getString("test_key");
}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    return null;
}
Ghassen
  • 591
  • 1
  • 15
  • 33