简体   繁体   English

在Spring Batch的Tasklet步骤中写入文件

[英]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. 应用程序将所有错误记录存储在Map中,最后需要将其写入文件中。 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 ? 有没有更好的方法可以将记录写入Tasklet中? rather than traditional FileStreamWriter ? 而不是传统的FileStreamWriter? Possible to use FileItemWriter inside tasklet? 可以在tasklet中使用FileItemWriter吗?

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. 如果将它们写入List ,则可以使用基于块的步骤,并使用ListItemReader和常规的FlatFileItemWriter作为编写器。

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. 我目前未使用SB,但我认为您也可以使用FlatFileItemWriter作为错误的侦听器,并设置shouldDeleteIfEmpty属性以使SB自动删除文件。
The only drawbacks for this solution are: 此解决方案的唯一缺点是:

  1. requires you to register FlatFileItemWriter as stream to let SB open/close correctly during chunk commit 要求您将FlatFileItemWriter注册为流,以便在块提交期间让SB正确打开/关闭

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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