Hi all,
I am a Spring Batch newbie. There are two parts to my question. I feel a bit silly with the first one, because it feels like there is probably an easy answer to this....I am just missing somethig and this is probably related to my lack of in-depth knowledge of Spring Batch.
I have records in a database and need to write them to a file. No problems here. I need a header and footer written to this file as well. It needs to be summary header and footer. Both the header and footer needs to have the total number of recods in. The footer also needs the total amount (each record has transaction amount).
So my questions:
1. I found an example of the summary footer. I tried to implement it. The footer gets written, but the amount wasn't updated. It looks like he "write" method is never reached. I have never written a custom reader and writer. I thought that maybe the class="" part should change to point to my new writer class, but then the property fields don't work anymore?
2. I have no idea how to do a summary header.
Code for part one of my question:
The custom writer (I only implemented the total for the amount so far. I will just add a counter for the other part needed for the footer).Code:<beans:bean id="ItemReader.ExportDataJob" class="org.springframework.batch.item.database.JdbcCursorItemReader"> <beans:property name="dataSource" ref="DataSourceBean.DEV.mhdev1"/> <beans:property name="sql" value="SELECT * FROM TEST WHERE STATUS=2"/> <beans:property name="rowMapper"> <beans:bean class="za.co.discovery.mapper.ValidatorFieldSetMapper"/> </beans:property> </beans:bean> <beans:bean id="FooterWriter.ExportDataJob" class="com.TrailWriter"> <beans:property name="delegate" ref="ItemWriter.ExportDataJob"/> </beans:bean> <beans:bean id="ItemWriter.ExportDataJob" class="org.springframework.batch.item.file.FlatFileItemWriter"> <beans:property name="resource" value="file:${data.extract.file.path}/${data.extract.file}_Export.txt"/> <beans:property name="lineAggregator"> <beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"> <beans:property name="delimiter" value="|"/> <beans:property name="fieldExtractor"> <beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"> <beans:property name="names" value="aval,bval,cval,amount"/> </beans:bean> </beans:property> </beans:bean> </beans:property> <beans:property name="footerCallback" ref="FooterWriter.ExportDataJob"/> </beans:bean>
Code:public class TrailWriter implements ItemWriter<ValidatorDto>, FlatFileFooterCallback{ private ItemWriter<ValidatorDto> delegate; private double totalAmount = 0.0; public void write(List<? extends ValidatorDto> items) throws Exception{ double chunkTotal = 0.0; System.out.println("****THIS CODE WAS ENTERED*********"); for (ValidatorDto dto: items){ chunkTotal += Double.parseDouble(dto.getTransactionAmount()); } delegate.write(items); totalAmount = totalAmount + chunkTotal; } public void writeFooter(Writer writer) throws IOException{ writer.write("CheckSum: " + totalAmount); } public void setDelegate(ItemWriter delegate){ this.delegate = delegate; } }


Reply With Quote
