简体   繁体   English

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

[英]Spring Batch. How to limit the no of executions of a chunk

I started to use spring batch very recently. 我最近开始使用弹簧批。 Can any body tell me how to limit the no of execution of a chunk (ie invocation of ItemReader and ItemWrite) within a tasklet. 任何正文都可以告诉我如何在tasklet中限制块的执行次数(即调用ItemReader和ItemWrite)。

I set the allow-start-if-complete="false", start-limit="1" in the tasklet. 我在tasklet中设置了allow-start-if-complete =“false”,start-limit =“1”。 Then I set commit-interval="1" in the chunk. 然后我在块中设置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>

My expectation is to run the tasklet/chunk only once for every batch job execution. 我的期望是每次批处理作业执行只运行一次tasklet / chunk。 But the behavior was the chunk(reader and writer) gets invoked several times/infinite. 但是行为是块(读者和作者)被多次/无限地调用。

Can anybody help me on this regards please. 请有人帮我解决这个问题。

Number of executions of a chunk depends on the reader ; 块的执行次数取决于reader ; Spring Batch does not control it. Spring Batch无法控制它。 If your reader reads from a database table, this limit will be the number of records returned from your SQL statement, or if it reads from a file it will be the number of lines (in the very basic cases) 如果您的读者从数据库表中读取,则此限制将是从SQL语句返回的记录数,或者如果它从文件中读取它将是行数(在非常基本的情况下)

start-limit controls the number of times a Step may be started, not the chunk configured for this step. start-limit控制Step可以启动的次数,而不是为此步​​骤配置的chunk。

add two parameters in your own reader: 在您自己的阅读器中添加两个参数:

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

and in the read method use this variable in order to control the flow: 并且在read方法中使用此变量来控制流程:

if(numberOfReading<maxNumberOfReading){

   // do what do yoou have to do

   numberOfReading++;

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

batch.maxNumberOfReading is setted in the property file, with 1 as value batch.maxNumberOfReading在属性文件中设置,值为1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM