春天批次 - 计数处理的行 [英] Spring Batch - Counting Processed Rows
问题描述
所以我创建一个Spring Batch作业,用于读取CSV文件和某些包含不完整数据的行;它检查,输出到日志,该行不完整,并跳过。它工作伟大,除了在工作结束时,我想要它记录多少行,它发现是不完整的。
我已经Google搜索了一个解决方案,但没有找到任何真正的东西。
$ b $任何帮助都是值得赞赏的,这是我怎么做的:
在ItemProcessor中,我添加了一个属性和一个方法从进程方法内访问ExecutionContext,
private ExecutionContext executionContext;
@BeforeStep
public void beforeStep(StepExecution stepExecution)
{
this.executionContext = stepExecution.getExecutionContext();
}
...然后在process我想要记录的行,我可以这样做,
this.executionContext.putInt(i_ThoseRows,this.executionContext。 getInt(i_ThoseRows,0)+ 1);
最后,我添加另一个方法到ItemProcessor以在步骤结束时打印结果, p>
@AfterStep
public void afterStep(StepExecution stepExecution)
{
System.out.println Number of'those rows':+ this.executionContext.getInt(i_ThoseRows,0));
}
希望它帮助某人
So I am creating a Spring Batch job for reading a CSV file and for certain rows which contain incomplete data; it checks, outputs to the log that the row is incomplete, and skips. It works great except at the end of the job I want it to log how many rows it found that were incomplete. Just something simple like "X incomplete rows were found".
I've Googled and searched around for a solution but not found anything really.
Any help is appreciated and any more info needed just ask.
Manage to solve this, here's how I did it:
In the ItemProcessor I added an attribute and a method for getting access to the ExecutionContext from within the process method,
private ExecutionContext executionContext;
@BeforeStep
public void beforeStep(StepExecution stepExecution)
{
this.executionContext = stepExecution.getExecutionContext();
}
...and then in the process() method when I find one of the rows I want to log, I can do this,
this.executionContext.putInt( "i_ThoseRows", this.executionContext.getInt( "i_ThoseRows", 0 ) + 1 );
Finally I add another method to the ItemProcessor to print the result at the end of the step,
@AfterStep
public void afterStep(StepExecution stepExecution)
{
System.out.println( "Number of 'Those rows': " + this.executionContext.getInt( "i_ThoseRows", 0 ) );
}
Hope it helps someone
这篇关于春天批次 - 计数处理的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!