Spring Batch - Passing Resource Name from MultiResourceItemReader > FlatFileItemReader to StepExecutionListener

天涯浪子 提交于 2019-12-11 11:47:55

问题


I have a Spring Batch job that needs to do the below

  • Check a directory on local file system that may contain more than one file
  • Process each of the files, save data from those files to database
  • Rename files by adding a suffix to include PROCESSED or ERROR

I have used the below

  • A MultiResourceItemReader that reads files and delegates to a FlatFileItemReader
  • The FlatFileItemReader reads data using LineMapper, FieldSetMapper
  • An ItemProcessor manipulates data read
  • An ItemWriter writes to the database

What I want to do

  • Rename each file to either PROCESSED / ERROR at the end of Step based on execution status
  • How do I pass the resource file name that the FlatFileItemReader processes to the StepExecutionListener?
  • How do I pass the resource file name to the ItemProcessor as it also needs to save the name of the file the data was read from

Below is my relevant config

<batch:job id="myJob" job-repository="jobRepository">
    <batch:step id="processFiles">
        <batch:tasklet>
            <batch:chunk reader="multiResourceReader" processor="myItemProcessor" writer="myItemWriter" commit-interval="100" />
        </batch:tasklet>
        <batch:listeners>
            <batch:listener ref="myStepListener"/>
        </batch:listeners>                          
    </batch:step>
</batch:job>

回答1:


This scenario is similar to this one.
I'm not a fan of first solution because is too messy and file rename is "hidden" into java code and can't be easily detected from job's xml definition.
I prefer solution #2: IMO success/error conditions are easy to check and step flow is more cleaner and well separated.
Get your consideration, I hope my advices will help.



来源:https://stackoverflow.com/questions/24817659/spring-batch-passing-resource-name-from-multiresourceitemreader-flatfileitem

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