Search code examples
springspring-bootflatfilereader

Spring Batch - How to store lines read from CSV into a execution context for Restartability


I am not sure if the FlatFileItemReader has the capability to store the last line when an exception is being met. So that when i re-run the batch application it will be able to continue from the last line.

Any example to implement the following use cause would be helpful Thanks!

 public static FlatFileItemReader<Employee> reader(String path){
    FlatFileItemReader<Employee> reader = new FlatFileItemReader<Employee>();

    reader.setResource(new ClassPathResource(path));
    reader.setLineMapper(new DefaultLineMapper<Employee>() {
    {
     setLineTokenizer(new DelimitedLineTokenizer() {
     {
       setNames(new String[] {"firstName", "lastName", "emailId"});
     }
   });
   setFieldSetMapper(new BeanWrapperFieldSetMapper<Employee>() {
     {
       setTargetType(Employee.class);
     }
   });
 }
});

return reader;
}}

Solution

  • I think It's a capability of the FlatFileItemReader

    DefaultLineMapper
    Now that the basic interfaces for reading in flat files have been defined, it becomes clear that three basic steps are required:
    
    Read one line from the file.
    

    It means that if you set chunk size to 1 in your job and reader fails on line 195, next time the job pick chunk no 195 and continue it from line no.195. Also at application start spring checks failed executions and restarts it automatically. Some docs:

    Retry

    Restartibility