'Implement A Spring Batch Progress Bar - Get total row count on Job Execution

I have a batch job which reads from a database using HibernateCursorItemReader, processes the results using a custom processor then commits to another database.

What I would like to show is how many items have been processed against total items to process.

I have tried implementing a custom JobExecutionListener with @beforeJob to get the row count from the first table which can then be compared against the Job Execution commits periodically.

Is there a better way than using a Job Execution Listener. is it possible to get the total row count for the table on the first read, setting a value on the HibernateCursorItemReader during initialization or something similar?

Job

<batch:job id="SomeLongJob" job-repository="jobRepository" restartable="true">      
    <batch:listeners>
        <batch:listener ref="batchJobExecutionListener" />
    </batch:listeners>  
    <batch:step id="first1">
        <tasklet transaction-manager="hibernateTransactionManager">
            <chunk reader="hibernateItemReader"
                   processor="batchCustomProcessor"
                   writer="hibernateItemWriter"
                   skip-limit="0"
                   commit-interval="10">
            </chunk>
        </tasklet>
    </batch:step>
</batch:job>

Reader

<bean id="hibernateItemReader"
    class="org.springframework.batch.item.database.HibernateCursorItemReader">
    <property name="queryString" value="from MyTable" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source