0

I have a spring batch program which reads from a database and writes to a file.

Job is:

<job id="MyTransactionJob" job-repository="jobRepository" incrementer="dynamicJobParameters">
<step id="TransactionfileGenerator">
        <tasklet transaction-manager="jobRepository-transactionManager">
            <chunk reader="MyItemReader" writer="MyItemWriter"  commit-interval="1000" skip-policy="skipPolicy"/>
        </tasklet>
        <listeners>
            <listener ref="MySkipListener"/>
    </listeners>
    </step> 
 </job>

Item Reader is:

<beans:bean id="MyItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <beans:property name="dataSource" ref="jobRepository-dataSource" />
    <beans:property name="sql" value="${dbTofileDataReadSQL}"/>
    <beans:property name="rowMapper">
        <beans:bean class="com.mypackage.MyRowMapper" />
    </beans:property>
</beans:bean>

dbTofileDataReadSQL is a simple select sql based on some condition. So if condition is not satisfied, 0 rows will be returned.

Item writer is:

<beans:bean id="MyItemWriter" class="com.mypackage.MyDbToFileItemWriter">
    <beans:property name="delegate">
        <beans:bean class="org.springframework.batch.item.file.FlatFileItemWriter">
            <beans:property name="resource" value="file:c:\output.dat" />
            <beans:property name="shouldDeleteIfExists" value="true"/>
            <beans:property name="lineAggregator">
                <beans:bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
            </beans:property>
        </beans:bean>
    </beans:property>
</beans:bean>

Issue is even if the number of rows returned are 0 an empty file will be created as the writer will always run.

Is it possible to put in a condition such that the file should be created if and only if there is atleast one row to be written. Else just skip the ItemWriter part completely.

Thanks for reading!!

1 Answer 1

1

the file will be opened when the step starts, you can

  • create a custom FlatFileItemWriter which uses the file in a lazy manner
  • create an afterstep which deletes the file if there are no written lines, this could be a more general solution, if you use a flow
  • use a simple shell script which checks the file after the job and deletes it if it is empty
Sign up to request clarification or add additional context in comments.

1 Comment

Pity that both FlatFileItemWriter#state and FlatFileItemWriter#resource are private (I would vote for protected or provide public getters). Otherwise one can implement "if state.getPosition() == 0 then resource.getFile().delete();" in FlatFileItemWriter#close().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.