Read all files from directory Spring batch FileReadingMessageSource

99封情书 提交于 2019-12-13 00:25:34

问题


I am using spring batch for my application. Basically, all I have to do is read all XML files from the input directory. When I get all the XML files, each file needs to be converted to root Object. That object I have to split into two objects and print them into CSV file. So, if there are 10 XML files in the input folder, I need to generate CSV with 20 lines. If there are 150, CSV will have 300 lines, etc. I meant to use FileReadingMessageSource. It has a queue. But the problem with this is the return type of that queue. It returns File, and what I need is the List of queues. Can I somehow get the entire list of XML files from a directory in my reader?


回答1:


IMO you don't need to use the FileReadingMessageSource (and introduce Spring Integration) in your basic use case.

You can create a reader that returns a File, a processor that maps the File to your root Object (and split in two objects) and finally a FlatFileItemWriter to generate the CSV output.

EDIT: add reader example:

@Bean
public ItemReader<File> itemReader() throws IOException {
    List<File> files = Files.walk(Paths.get("/path/to/directory"))
            .filter(Files::isRegularFile)
            .map(Path::toFile)
            .collect(Collectors.toList());
    return new IteratorItemReader<>(files);
}


来源:https://stackoverflow.com/questions/53854446/read-all-files-from-directory-spring-batch-filereadingmessagesource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!