I am a newbie to spring batch, i would like to find the perfect approach to use with the use case as shown here:
I have multiple csv files that i would like to store them on Memory ( as Collection Store .. i.e List Map) then i would like to use/refer to them on next steps/jobs for my logic business.
Let's take one example an Object XX stored on Map with an ItemWriter.
Object XX Model
public class Object {
private int x;
private int y;
// getters setters
}
the itemReader for Object X
public class ObjectItemReader extends FlatFileItemReader<Object> {
public ObjectItemReader() {
this.setResource(new ClassPathResource("xxx.csv"));
this.setLineMapper(new DefaultLineMapper<Object>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "x", "y" });
setDelimiter(DELIMITER_TAB);
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Object>() {{
setTargetType(Object.class);
}});
}});
}
}
the ObjectWriter
public class ObjectItemWriter implements ItemWriter<Object> {
private Map<Long , Object> objectMap;
public ObjectItemWriter() {
System.out.println("Map Store is created ");
objectMap= new HashMap<Long , Object>();
}
@Override
public void write(List<? extends Object> items) throws Exception {
for (Object depot : items) {
objectMap.put(depot.getX(), depot);
}
}
public Map<Long , Object> getobjectMap() {
return objectMap;
}
}
as you can see all records are stored in Map with the itemWriter, i made a test with a simple tasklet to access this Map on other steps
public class TaskletStep implements Tasklet{
@Autowired
private ObjectItemWriter objectItemWriter;
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println(objectItemWriter.getobjectMap().size());
return null;
}
}
My questions,
is there another way / best way to store all csv files on memory with using only ItemReader or ItemProcessor as it's a simple getting data from files to Map?
Is that the itemWriter is an essential step to store these filed on Map?