Spring Batch:填充n个文件但下一步看空

时间:2015-01-12 23:57:31

标签: java spring spring-batch

我在spring批处理中实现了一个步骤,它从数据库中读取记录并生成输出文件。 对该步骤进行分区,以便每个从属步骤生成不同的文件。 然后我实现了一个在奴隶之后执行的步骤,它的作用是阅读每个步骤 生成文件并将它们合并到一个文件中(合并)。

我遇到的问题是,当运行整合器步骤时,输入文件为空(可能是因为 没有执行刷新),然后尝试在从属编写器中配置forceSync = true和transactional = false,但没有效果。

当他们完成作业时,从属生成的文件包含数据,合并文件为空。 那么问题是,在执行合并器步骤时输入文件是空的。

可以帮助我吗?需要更多信息吗?

(对不起我的英文)

感谢

<batch:job id="interfacesJob" xmlns="">
    <batch:step id="step.1" parent="readerParent" next="step.2"/>
    <batch:step id="step.2" parent="merge"/>
</batch:job>

<batch:step id="readerParent">
    <batch:partition step="slave" partitioner="partitioner">
        <batch:handler grid-size="50" task-executor="poolTaskExecutor" />
    </batch:partition>
</batch:step>

<batch:step id="slave">
    <batch:tasklet 
        transaction-manager="transactionManager">
        <batch:chunk
            reader="dummyReader" 
            processor="dummyProcessor"
            writer="dummyWriter" 
            commit-interval="1"
            skip-limit="50">
    </batch:tasklet>
</batch:step>

<bean id="dummyWriter"
      class="org.springframework.batch.item.file.FlatFileItemWriter"
      scope="step" >
    <property name="resource" value="file:operations_#{stepExecutionContext[fromId]}.txt" />
    <property name="forceSync" value="true" />
    <property name="transactional" value="false" />
    <property name="lineAggregator">
        <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
            <property name="delimiter" value="," /> <!-- default -->
            <property name="fieldExtractor">
                <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                    <property name="names" value="id,field1,field2" />
                </bean>
            </property>
        </bean>
        </property>
</bean>

<batch:step id="merge">
    <tasklet>
        <chunk reader="mergeReader" writer="mergeWriter" commit-interval="1" />
    </tasklet>
</batch:step>

<bean id="mergeReader" 
       class="org.springframework.batch.item.file.FlatFileItemReader">
       <property name="resources" value="file:operations_*.txt" />
       <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
          <property name="lineTokenizer">
            <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                <property name="delimiter" value="," />
            <property name="names" value="id,field1,field2" />
            </bean>
              </property>
              <property name="fieldSetMapper">
            <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
            <property name="prototypeBeanName" value="dummyPrototypeBean" />
            </bean>
              </property>
       </bean>
      </property>
</bean>

<bean id="mergeWriter"
      class="org.springframework.batch.item.file.FlatFileItemWriter">
      <property name="resource" value="file:final.txt" />
      <property name="lineAggregator">
    <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
        <property name="delimiter" value="," /> 
        <property name="fieldExtractor">
            <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                <property name="names" value="id,field1,field2" />
            </bean>
        </property>
    </bean>
      </property>
</bean>

我尝试使用mergeRader的MultiResourceItemReader,但仍然继续存在同样的问题:

<bean id="mergeReader"
 class="org.springframework.batch.item.file.MultiResourceItemReader">
   <property name="resources" value="file:operations_*.txt" />
   <property name="delegate" ref="mergeReaderSpecific" />
 </bean>



<bean id="mergeReaderSpecific" 
      class="org.springframework.batch.item.file.FlatFileItemReader">
       <property name="lineMapper">
           <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
               <property name="lineTokenizer">
                  <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                      <property name="delimiter" value="," />
                      <property name="names" value="id,field1,field2" />
                   </bean>
               </property>
               <property name="fieldSetMapper">
                  <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                      <property name="prototypeBeanName" value="dummyPrototypeBean" />
                  </bean>
               </property>
          </bean>
       </property>
</bean>

1 个答案:

答案 0 :(得分:0)

您可能必须使用mergeReader FlatFileItemReader作为代理FlatFileItemReader.resource。{ MultiResourceItemReader.resources属性旨在用于与之相对的单个资源 {{1}}(复数)。