简体   繁体   中英

Writing to a file in Tasklet step in Spring batch

My requirement is to write all the error records into a file after batch execution. Applications stores all error records in a Map and finally it needs to be written into a file. After batch core step is done, I call this tasklet as a nextstep. Is there a better way to write the records in the tasklet ? rather than traditional FileStreamWriter ? Possible to use FileItemWriter inside tasklet?

That really depends on how you store the records. If you write them to a List , you could use a chunk based step with a ListItemReader and a regular FlatFileItemWriter as the writer.

It might be write error file during batch processing and delete file in nextstep if it is empty a better approach?

I'm currently not using SB but I think you can also use a FlatFileItemWriter as listener for errors and set shouldDeleteIfEmpty property to let SB delete file automatically.
The only drawbacks for this solution are:

  1. requires you to register FlatFileItemWriter as stream to let SB open/close correctly during chunk commit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM