Spring Batch - 计算已处理的行数 [英] Spring Batch - Counting Processed Rows

查看:40
本文介绍了Spring Batch - 计算已处理的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个 Spring Batch 作业来读取 CSV 文件和包含不完整数据的某些行;它会检查,将行不完整输出到日志,然后跳过.除了在工作结束时我希望它记录它发现不完整的行数之外,它工作得很好.只是一些简单的事情,比如发现 X 行不完整".

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:

在 ItemProcessor 中,我添加了一个属性和一个方法,用于从 process 方法中访问 ExecutionContext,

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();
}

...然后在 process() 方法中,当我找到要记录的行之一时,我可以这样做,

...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 );

最后我在 ItemProcessor 中添加了另一个方法以在步骤结束时打印结果,

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 ) );
}

希望对大家有所帮助

这篇关于Spring Batch - 计算已处理的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆