【问题标题】:FlatFileItemWriter not generating the file when using Tasklet approach使用 Tasklet 方法时,FlatFileItemWriter 不生成文件
【发布时间】:2020-07-20 22:55:26
【问题描述】:

我使用tasklet 方法编写了以下代码来生成包含数据的文件。

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

上面的代码编译并运行没有错误,但它没有在磁盘上生成任何文件。

我尝试调试以检查是否在缓冲区上创建了 fileName 和 etc 值以写入磁盘,并且除了生成数据并将数据写入文件之外,一切都按预期工作。

如果我使用基于块的方法编写代码,它工作正常。

如果我做错了什么,请告诉我。提前感谢您的帮助。

编辑: 添加建议的更改后,文件正在磁盘上创建,但文件缺少我使用设置的标题setHeaderCallback()

【问题讨论】:

    标签: spring-batch batch-processing spring-batch-tasklet tasklet itemwriter


    【解决方案1】:

    在您的write 方法中,您创建一个FlatFileItemWriter 的实例,在其上设置一些属性,然后在其上调用close

    您没有调用open()write() 方法,这就是它没有生成文件的原因。

    【讨论】:

    • 我按照你说的做了。现在文件正在生成,但文件为空。我已经编辑了关于我如何做到这一点的问题。请让我知道我哪里出错了。
    • 我添加了 flatFileWriter.setShouldDeleteIfEmpty(true);并将数据打印到文件中。但它错过了我用 setHeaderCallback() 设置的标题
    • 你必须调用 flatFileWriter.write(),而不是 doWrite()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    相关资源
    最近更新 更多