Chris311
Chris311

Reputation: 3992

Number of lines read with Spring Batch ItemReader

I am using SpringBatch to write a csv-file to the database. This works just fine.

I am using a FlatFileItemReader and a custom ItemWriter. I am using no processor.

The import takes quite some time and on the UI you don't see any progress. I implemented a progress bar and got some global properties where i can store some information (like lines to read or current import index).

My question is: How can i get the number of lines from the csv?

Here's my 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>

I already tried to use the ItemReadListener Interface, but this isn't possible as well.

Upvotes: 0

Views: 5234

Answers (1)

Arnaud Potier
Arnaud Potier

Reputation: 1780

if you need to know how many lines where read, it's available in spring batch itself, take a look at the StepExecution

The method getReadCount() should give you the number you are looking for.

You need to add a step execution listener to your step in your xml configuration. To do that (copy/pasted from spring documentation):

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

where "chunkListner" is a bean of yours annotated with a method annotated with @AfterStep to tell spring batch to call it after your step.

you should take a look at the spring reference for step configuration

Hope that helps,

Upvotes: 1

Related Questions