简体   繁体   中英

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.

I set the allow-start-if-complete="false", start-limit="1" in the tasklet. Then I set commit-interval="1" in the chunk.

<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. 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 ; Spring Batch does not control it. 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)

start-limit controls the number of times a Step may be started, not the chunk configured for this step.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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