We help IT Professionals succeed at work.
Get Started

Spring Batch: Include count of records in File Header.

202 Views
Last Modified: 2020-01-01
I am writing a standalone application using Spring Batch. The below is my batch job configuration:
<batch:job id="sampleBatchJob">
        <batch:step id="createFile">
            <tasklet>
                <batch:chunk reader="sampleReader" writer="sampleFileWriter" processor="sampleRecordProcessor" commit-interval="500"/>
            </tasklet>
        </batch:step>
        <batch:listeners>
            <batch:listener ref="sampleStepExecutionListener" />
            <batch:listener ref="executionContextPromotionListener" />
        </batch:listeners>
    </batch:job>
    <bean id="executionContextPromotionListener"
          class="org.springframework.batch.core.listener.ExecutionContextPromotionListener">
        <property name="keys" value="recordCount" />
    </bean>
    <bean id="outputResource" class="org.springframework.core.io.ClassPathResource">
        <constructor-arg type="java.lang.String" value="sample.csv" />
    </bean>

    <bean id="sampleReader" class="test.SampleDataItemReader" scope="step"/>

    <bean id="sampleFileWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
        <property name="resource" ref="outputResource"/>
        <property name="shouldDeleteIfExists" value="true" />
        <property name="headerCallback" ref="sampleFileWriterCallback" />
        <property name="lineAggregator">
            <bean class="test.SampleCustomLineAggregator" >
                <property name="delimiter" value="," />
            </bean>
        </property>
    </bean>

    <bean id="sampleFileWriterCallback" class="test.SampleFileHeaderWriterCallback"/>

    <bean id="sampleRecordProcessor" class="test.SampleRecordProcessor"/>

    <bean id="sampleStepExecutionListener" class="test.SampleStepExecutionListener"/>

Open in new window


I have to include the count of records in the File Header and for the same I have declared a property "recordCount" in executionContextPromotionListener. I am setting the value for the property in the ItemReader implementation using the ExecutionContext.

But before the ItemReader is invoked, the Header Callback for the ItemWriter is invoked and the writeHeader(Writer) is executed. Also, I do not have access to the ExecutionContext object when the writeHeader(Writer) is executed.

Any suggestions, how I can pass the value from the reader to the FileHeaderWriterCallBack implementation?
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE