Spring批量读取文件与相关记录

时间:2013-03-07 08:54:07

标签: spring batch-processing spring-batch

我正在使用spring批处理来读取平面文件。该文件有相关记录。即,可以有父记录和任何数量的子记录,我想读取所有记录并调用Web服务来存储它。我也想捕获关系并存储它。一个挑战是子记录可以在文件中的任何位置。孩子也可以有很多孩子的记录。我无法找到春季批次的这个问题的解决方案。 请提供您的建议

更新:我没有任何选择将数据库用作临时数据存储。

1 个答案:

答案 0 :(得分:0)

我通过多次处理文件解决了这个问题。

在每一次传递中,我都会尝试使用这样的alg:

来读取\处理文件中的每个记录
  • 如果记录有父级 - 检查父级是否已存储。如果不是 - 我在处理器中跳过它
  • 如果记录未更改(或者如果无法更新则已存储) - 在处理器中跳过它
  • else - 存储在db

然后声明循环和决策:

    <batch:step id="processParentAndChilds" next="loop">
        <batch:tasklet>
            <batch:chunk reader="processParentAndChildsReader"
                         commit-interval="1000">
                <batch:processor>
                    <bean class="processParentAndChildsProcessor"/>
                </batch:processor>
                <batch:writer>
                    <bean class="processParentAndChildsWriter"/>
                </batch:writer>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
    <batch:decision decider="processParentAndChildsRetryDecider" id="loop">
        <batch:next on="NEXT_LEVEL" to="processprocessParentAndChilds"/>
        <batch:next on="COMPLETED" to="goToNextSteps"/>
    </batch:decision> 


public class ProcessParentAndChildsRetryDecider implements JobExecutionDecider{
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
    // if no on record written - no sense to try again
    if (stepExecution.getWriteCount() > 0) {
        return new FlowExecutionStatus("NEXT_LEVEL");
    } else {
        return FlowExecutionStatus.COMPLETED;
    }
}

}