FlatFileItemWriter not generating the file when using Tasklet approach

708 views Asked by At

I wrote the following code using tasklet approach to generate a file with data.

    public class PersonInfoFileWriter implements Tasklet {
        @Autowired
        PersonInfoFileUtil personInfoFileUtil;
    
        public void write(ExecutionContext executionContext) throws IOException {
List<PersonInfo> personInfoList = null;
            FlatFileItemWriter<PersonInfo> flatFileWriter = new FlatFileItemWriter<PersonInfo>();
flatFileWriter.setResource(new FileSystemResource("C:\\test\\"
                        + LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) + ".txt"));
            try {
flatFileWriter.open(executionContext);
                String personName = (String) executionContext.get("personInfo");
                //gets the details of the person by name from the database and assign the values to PersonInfo
                personInfoList  = personInfoFileUtil.setDataForPersonInfoFile(personName);
                
    
                flatFileWriter.setName("Person-Detail-File");
                flatFileWriter.setShouldDeleteIfEmpty(true);
                flatFileWriter.setAppendAllowed(true);
                flatFileWriter.setLineSeparator("\n");
                flatFileWriter.setHeaderCallback(new FlatFileHeaderCallback() {
                    @Override
                    public void writeHeader(Writer writer) throws IOException {
                        writer.write(
                                "PersonId^Name^Program^ProgramType");
                    }
                });
                flatFileWriter.setLineAggregator(new DelimitedLineAggregator<PersonInfo>() {
                    {
                        setDelimiter("^");
                        setFieldExtractor((FieldExtractor<PersonInfo>) new BeanWrapperFieldExtractor<PersonInfo>() {
                            {
                                setNames(new String[] { "personId", "name", "program", "programType" });
                            }
                        });
                    }
                });
                String lines = flatFileWriter.doWrite((List<? extends PersonInfo>) personInfoList);
                logger.info(lines); //this prints the information correctly
            } finally {
                flatFileWriter.close();
            }
            
        }
    
        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            ExecutionContext executionContext = contribution.getStepExecution().getJobExecution().getExecutionContext();
            write(executionContext);
            return RepeatStatus.FINISHED;
        }
    }

The above code compiles and runs without errors but it is not generating any file on to the disk.

I tried debugging to check if the fileName and etc values are getting created on to a buffer to write to a disk and everything works as intended except generating and writing the data to a file.

If I write the code using chunk based approach it is working fine.

Please let me know if I am doing any mistake. Thanks for the help in advance.

EDIT: after adding the changes that were suggested the file is getting created on the disk but the file is missing out the header that I have set using setHeaderCallback()

1

There are 1 answers

3
Mahmoud Ben Hassine On BEST ANSWER

In your write method, you create an instance of FlatFileItemWriter, set some properties on it and then call close on it.

You did not call open() and write() methods, that's why it is not generating an file.