Spring批处理 - 从MultiResourceItemReader传递资源名称> FlatFileItemReader到StepExecutionListener [英] Spring Batch - Passing Resource Name from MultiResourceItemReader > FlatFileItemReader to StepExecutionListener

查看:1218
本文介绍了Spring批处理 - 从MultiResourceItemReader传递资源名称> FlatFileItemReader到StepExecutionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Batch作业,需要做以下工作:


  • 检查本地文件系统中可能包含多个目录的目录文件

  • 处理每个文件,将这些文件中的数据保存到数据库中
  • 通过添加后缀重命名文件以包含PROCESSED或ERROR
  • >


我已经使用了下面的




  • A MultiResourceItemReader 读取文件并委托给 FlatFileItemReader

  • FlatFileItemReader 使用 LineMapper em>, FieldSetMapper

  • ItemProcessor 操作数据读取
  • 写入数据库


我想要做什么
$ b



  • 根据执行状态将每个文件重命名为步骤结束时的PROCESSED / ERROR
  • 如何传递 FlatFileItemReade的资源文件名r 处理到 StepExecutionListener
  • 如何将资源文件名传递给 ItemProcessor 保存读取数据的文件的名称



下面是我的相关配置

 < batch:job id =myJobjob-repository =jobRepository> 
< batch:step id =processFiles>
< batch:tasklet>
< batch:chunk reader =multiResourceReaderprocessor =myItemProcessorwriter =myItemWritercommit-interval =100/>
< / batch:tasklet>
< batch:listeners>
< batch:listener ref =myStepListener/>
< / batch:listeners>
< / batch:job>


解决方案

这种情况类似于
我更喜欢解决方案#2:海事组织的成功/错误条件很容易检查,步骤流程更清晰,分离。

请考虑,我希望我的建议能够帮到你。

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>

解决方案

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.

这篇关于Spring批处理 - 从MultiResourceItemReader传递资源名称&gt; FlatFileItemReader到StepExecutionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆