0

I use spring batch in order to do the workflow below:

  • step 1: read a large CSV and put it into Map
  • step 2: from the previous map I have to do a business logic.

I created a bean named CourseCsvRepository in order to save the mapped csv to this map (Singleton bean):

@Component
public class CourseCsvRepository {

    private Map<String, List<Course>> courseMappedByKey = new HashMap<>();


    public void addToMap(Course course){

        String key = "";
        key= key.concat(String.valueOf(course.getCodeStifLigne())).concat(course.getAntenne()).concat(String.valueOf(course.getIdtm())).concat(String.valueOf(course.getIdmiss())).concat(String.valueOf(course.getCourse())).concat(course.getNommiss());

        if(courseMappedByKey.containsKey(key)){
            courseMappedByKey.get(key).add(course);
        }else {
            final ArrayList<Course> list = new ArrayList<>();
            list.add(course);
            courseMappedByKey.put(key, list);
        }
    }

    public Map<String, List<Course>> getMap(){
        return this.courseMappedByKey;
    }

    public List<Course> getByKey(String key){
        return this.courseMappedByKey.get(key);
    }


}

my second reader (Of step 2) is as bellow:

@Bean
public ItemReader<List<Course>> readFromMap(){

    ListItemReader<List<Course>> reader = new ListItemReader<List<Course>>(new ArrayList(courseCsvRepository.getMap().values()));

    return reader;
}

but always courseCsvRepository.getMap() return null, because my bean is created before I do step 1(which used to fill our map)

@Bean
public Job writeCsvToDbJob() {
    return jobBuilderFactory.get("writeCsvToDbJob")
            .incrementer(new RunIdIncrementer())
            .start(step1())
            .next(step2())
            .build();
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
BERGUIGA Mohamed Amine
  • 6,094
  • 3
  • 40
  • 38

1 Answers1

0

I want to share with you the answer of the previous probelem:

my beans need to be step scoped so that they will be able to receive the stepExecutionContext params, at every step. If they would not be step scoped, their beans will be created initially, and won't accept the filled map at step level.

@StepScope: How Does Spring Batch Step Scope Work

@Bean
@StepScope
public ItemReader<List<Course>> readFromMap(){

    ListItemReader<List<Course>> reader = new ListItemReader<List<Course>>(new ArrayList(courseCsvRepository.getMap().values()));

    return reader;
}
BERGUIGA Mohamed Amine
  • 6,094
  • 3
  • 40
  • 38