多输入文件Spring Batch

时间:2014-02-03 12:22:10

标签: spring spring-batch executioncontext

我正在尝试开发一个可以使用Spring Batch处理包含文件的目录的批处理 我查看了 MultiResourcePartitioner 并尝试了一些像:

<job parent="loggerParent" id="importContractESTD" xmlns="http://www.springframework.org/schema/batch">
    <step id="multiImportContractESTD">
        <batch:partition step="partitionImportContractESTD" partitioner="partitioner">
            <batch:handler grid-size="5" task-executor="taskExecutor" />
        </batch:partition>
    </step>
</job>

<bean id="partitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner">
    <property name="keyName" value="inputfile" />
    <property name="resources" value="file:${import.contract.filePattern}" />
</bean>

<step id="partitionImportContractESTD" xmlns="http://www.springframework.org/schema/batch">
    <batch:job ref="importOneContractESTD" job-parameters-extractor="defaultJobParametersExtractor" />
</step>

<bean id="defaultJobParametersExtractor" class="org.springframework.batch.core.step.job.DefaultJobParametersExtractor"
    scope="step" />

<!-- Job importContractESTD definition -->
<job parent="loggerParent" id="importOneContractESTD" xmlns="http://www.springframework.org/schema/batch">
    <step parent="baseStep" id="initStep" next="calculateMD5">
        <tasklet ref="initTasklet" />
    </step>
    <step id="calculateMD5" next="importContract">
        <tasklet ref="md5Tasklet">
            <batch:listeners>
                <batch:listener ref="md5Tasklet" />
            </batch:listeners>
        </tasklet>
    </step>
    <step id="importContract">
        <tasklet>
            <chunk reader="contractReader" processor="contractProcessor" writer="contractWriter" commit-interval="${commit.interval}" />
            <batch:listeners>
                <batch:listener ref="contractProcessor" />
            </batch:listeners>
        </tasklet>
    </step>
</job>

<!-- Chunk definition : Contract ItemReader -->
<bean id="contractReader" class="com.sopra.banking.cirbe.acquisition.batch.AcquisitionFileReader" scope="step">
    <property name="resource" value="#{stepExecutionContext[inputfile]}" />
    <property name="lineMapper">
        <bean id="contractLineMappe" class="org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper">
            <property name="tokenizers">
                <map>
                    <entry key="1*" value-ref="headerTokenizer" />
                    <entry key="2*" value-ref="contractTokenizer" />
                </map>
            </property>
            <property name="fieldSetMappers">
                <map>
                    <entry key="1*" value-ref="headerMapper" />
                    <entry key="2*" value-ref="contractMapper" />
                </map>
            </property>
        </bean>
    </property>
</bean>

<!-- MD5 Tasklet -->
<bean id="md5Tasklet" class="com.sopra.banking.cirbe.acquisition.batch.AcquisitionMD5Tasklet">
    <property name="file" value="#{stepExecutionContext[inputfile]}" />
</bean>

但我得到的是:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'stepExecutionContext' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'

我正在寻找的方法是为文件中包含的每个文件启动我的作业 importOneContractESTD :$ {import.contract.filePattern} 。并且每个文件在步骤 calculateMD5 (将处理后的文件md5放入我的jobContext)和步骤 importContract (从jobContext中读取前一个md5以添加)之间共享它作为contractProcessor处理的每一行的数据)

如果我只尝试使用一个作为参数给出的文件调用importOneContractESTD(例如,替换 $ {my.file} #{stepExecutionContext [inputfile]} ),它工作...但我想尝试使用spring批处理来管理我的目录而不是我的调用shell脚本......

感谢您的想法!

1 个答案:

答案 0 :(得分:0)

当您需要访问stepExecutionContext

时,添加scope =“step”

喜欢这里:

<bean id="md5Tasklet" class="com.sopra.banking.cirbe.acquisition.batch.AcquisitionMD5Tasklet" scope="step">
    <property name="file" value="#{stepExecutionContext[inputfile]}" />
</bean> 

更多信息here