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