我有一个db to file job。
项目编写者如下:
<beans:bean id="myReportFooterCallback" class="com.myPackage.util.MyReportFooterCallback">
<beans:property name="delegate" ref="myReportFileItemWriter" />
</beans:bean>
<beans:bean id="myReportFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<beans:property name="resource" value="file:c:\TestData\output_report.dat" />
<beans:property name="lineAggregator">
<beans:bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<beans:property name="names" value="BUSINESS_DATE, FIELD_ONE, FIELD_TWO, FIELD_THREE, FIELD_FOUR, FIELD_FIVE, FIELD_SIX " />
</beans:bean>
</beans:property>
<beans:property name="format" value="%-12s%-20s%-20s%-20s%-5s%-5s%-10s" />
</beans:bean>
</beans:property>
<beans:property name="footerCallback" ref="myReportFooterCallback" />
</beans:bean>
步骤如下:
<step id="ReportFileGenerator">
<tasklet transaction-manager="jobRepository-transactionManager">
<chunk reader="ReportFileItemReader" writer="myReportFooterCallback" commit-interval="1000"/>
</tasklet>
</step>
MyReportFooterCallback类如下:
public class MyReportFooterCallback implements ItemWriter<MyReportBean>, FlatFileFooterCallback {
private ItemWriter<MyReportBean> delegate;
private double totalDebitAmount = 0.0;
private double totalCreditAmount = 0.0;
private int totalDebit = 0;
private int totalCredit = 0;
public void write(final List<? extends MyReportBean> items) throws Exception {
System.out.println("in write method!");
int chunkDebitCount = 0;
int chunkCreditCount = 0;
double chunkDebitTot = 0.0;
double chunkCreditTot = 0.0;
for (MyReportBeanitem : items) {
if (item.getIndicator().equalsIgnoreCase("Debit")) {
// its a debit
chunkDebitCount = chunkDebitCount + 1;
chunkDebitTot = chunkDebitTot + Double.parseDouble(item.getTRANS_AMT());
} else {
// its a credit
chunkCreditCount = chunkCreditCount + 1;
chunkCreditTot = chunkCreditTot + Double.parseDouble(item.getTRANS_AMT());
}
}
this.delegate.write(items);
this.totalDebit = this.totalDebit + chunkDebitCount;
this.totalDebitAmount = this.totalDebitAmount + chunkDebitTot;
this.totalCredit = this.totalCredit + chunkCreditCount;
this.totalCreditAmount = this.totalCreditAmount + chunkCreditTot;
}
public void writeFooter(final Writer writer) throws IOException {
writer.write("\n");
writer.write("\n");
writer.write("Total Debits Processed: " + this.totalDebit);
writer.write("\n");
writer.write("Total Debit Amount: " + this.totalDebitAmount);
writer.write("\n");
writer.write("\n");
writer.write("Total Credits Processed: " + this.totalCredit);
writer.write("\n");
writer.write("Total Credit Amount: " + this.totalCreditAmount);
}
public void setDelegate(final ItemWriter<MyReportBean> delegate) {
this.delegate = delegate;
}
}
但是,上面的代码没有按预期工作:
1)使用七列生成文件output_report.dat。但是,页脚不显示总值。所有四个值都是0(计数和总数)。
我错过了什么?
2)另外,是否可以将列名添加到每列的顶部?
3)有任何方法可以将页码添加到输出报告中吗?
感谢阅读!
答案 0 :(得分:0)
使用类似示例的快速尝试对我来说没有问题*(即使没有步骤范围)
1)您是否尝试过JUnit测试以确定它有效?下一步是调试/记录
2)确定,也存在HeaderCallback
3)没有开箱即用,但实现它不应该太难,使用totalCount,除以pagesize,如果结果是整数则打印项目
(*)您的示例似乎不完整,我必须将myReportFileItemWriter注册为Stream