春批。如何限制块的执行次数

时间:2012-06-20 11:58:13

标签: spring spring-batch

我最近开始使用弹簧批次。任何正文都可以告诉我如何在一个tasklet中限制一个块的执行次数(即调用ItemReader和ItemWrite)。

我在tasklet中设置了allow-start-if-complete =“false”,start-limit =“1”。然后我在块中设置commit-interval =“1”。

<batch:step id="mig-chain-data">
<batch:tasklet allow-start-if-complete="false" start-limit="1">
<batch:chunk commit-interval="1" reader="reader" writer="writer"></batch:chunk>
</batch:tasklet>
</batch:step>

我的期望是每次批处理作业执行只运行一次tasklet / chunk。但是行为是块(读取器和写入器)被多次/无限地调用。

请有人帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

块的执行次数取决于reader; Spring Batch无法控制它。 如果您的读者从数据库表中读取,则此限制将是从SQL语句返回的记录数,或者如果它从文件中读取它将是行数(在非常基本的情况下)

start-limit控制Step可以启动的次数,而不是为此步​​骤配置的chunk。

答案 1 :(得分:0)

在您自己的阅读器中添加两个参数:

private static int numberOfReading=0;
@Value("${batch.maxNumberOfReading}")
private int maxNumberOfReading;

并且在read方法中使用此变量来控制流程:

if(numberOfReading<maxNumberOfReading){

   // do what do yoou have to do

   numberOfReading++;

   // return the result to the writer
}
return null;

batch.maxNumberOfReading在属性文件中设置,值为1