使用Spring Batch ItemReader读取的行数

时间:2014-02-13 09:49:32

标签: csv spring-batch

我正在使用SpringBatch将csv文件写入数据库。这很好用。

我使用的是FlatFileItemReader和自定义ItemWriter。我没有使用处理器。

导入需要相当长的时间,在UI上您看不到任何进展。我实现了一个进度条,并获得了一些全局属性,我可以存储一些信息(如读取行或当前导入索引)。

我的问题是:如何从csv获得行数?

这是我的xml:

<batch:job id="importPersonsJob" job-repository="jobRepository">
    <batch:step id="importPersonStep">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="personItemReader"
                         writer="personItemWriter"
                         commit-interval="5"
                         skip-limit="10">
                <batch:skippable-exception-classes>
                    <batch:include class="java.lang.Throwable"/>
                </batch:skippable-exception-classes>             
            </batch:chunk>
            <batch:listeners>
                <batch:listener ref="skipListener"/>
                <batch:listener ref="chunkListener"/>
            </batch:listeners>
        </batch:tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="authenticationJobListener"/>
        <batch:listener ref="afterJobListener"/>
    </batch:listeners>
 </batch:job>

我已经尝试使用ItemReadListener接口,但这也是不可能的。

1 个答案:

答案 0 :(得分:1)

如果你需要知道读取了多少行,它可以在春季批次中找到, 看看StepExecution

方法getReadCount()应该为您提供所需的数字。

您需要在xml配置中为步骤添加步骤执行侦听器。要做到这一点(从弹簧文档中复制/粘贴):

<step id="step1">
    <tasklet>
        <chunk reader="reader" writer="writer" commit-interval="10"/>
        <listeners>
            <listener ref="chunkListener"/>
        </listeners>
   </tasklet>
</step>

其中“chunkListner”是你的bean,用@AfterStep注释的方法注释,告诉spring批处理你的步骤之后调用它。

你应该看看spring reference for step configuration

希望有所帮助,

相关问题