Batch Idioms
A batch job usually takes in homogeneous inputs which are really huge and processes it. There are so many batch idioms which are very important and any framework should be able to address these as they form the core base.
Repeat
Any batch framework should be able to iterate and do a common concern multiple times. “Repeat” is the batch idiom which addresses this.
Spring Batch performs iteration over the input and is done using org.springframework.batch.repeat.support.RepeatTemplate with org.springframework.batch.repeat.RepeatCallback as shown pictorially in Figure 23-15 below.
Figure 23-15. Working of RepeatTemplate with RepeatCallback
Retry
Due to transient errors during processing may require “Retry” of an input item. Any batch framework should be able to address this concern. Spring Batch supports doing this in a comprehensive manner and various levels of configurations/setup are possible. With Spring Batch you have a provision to mark certain exceptions as retry-able and can also configure the maximum number of retries per step execution. Listing 23-25 below shows the configuration which can be put in the step of a job and shows setting up of an exception as retry-able and that maximum three times it can be retried.
Listing 23-25. Retry-able step configuration in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<step id="step1"> <tasklet> <chunk reader="itemReader" writer="itemWriter" commit-interval="10" retry-limit="3"> <retryable-exception-classes> <includeCodeCxSpMiddle" style="margin-left:144.0pt;mso-add-space:auto">FlatFileFormatException"/> </retryable-exception-classes> </chunk> </tasklet> </step> |
Skip
It could be that due to some reason some inputs are invalid, which we may want to “Skip” and move forward with other items. In most of the cases not all processing errors occurring while execution can be considered as a failure of the entire job because of which this functionality of “Skip” is very relevant. Spring Batch addresses this without any problem. Similar to “Repeat” ship can also be configured as shown in Listing 23-26 below.
Listing 23-26. Retry-able step configuration in Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<step id="step1"> <tasklet> <chunk reader="itemReader" writer="itemWriter" commit-interval="10" skip-limit="10"> <skippable-exception-classes> <includeCodeCxSpMiddle" style="margin-left:144.0pt;mso-add-space:auto">FlatFileFormatException"/> </skippable-exception-classes> </chunk> </tasklet> </step> |
Restart
Some errors however should fail the job execution and then allow fixing the problem and then “Restart” it where it was left. By default jobs configured using Spring Batch namespace is restartable in nature. If you want to avoid this use the attribute restartable=”false”. For restarting jobs where it was left requires job and step executions to be persisted, otherwise job executions would always start from beginning. Figure 23-16 below shows restarting of job in a pseudo-code manner. Listing 23-27 shows restart configuration in Spring configuration file.
Figure 23-16. Restartability
Listing 23-27.Restart configuration in Spring configuration file in which steps will run again on restart (by default not allowed)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<step id="stepOne"> <tasklet allow-start-if-complete="true"> <chunk reader="itemReader" writer="itemWriter" commit-interval="10"> </chunk> </tasklet> </step> |
Listing 23-28. Restart configuration in Spring configuration file in which you can limit the number of step restart allowed (default is unlimited)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<step id="stepTwo"> <tasklet start-limit="2"> <chunk reader="itemReader" writer="itemWriter" commit-interval="10"> </chunk> </tasklet> </step> |
Page Visitors: 25402


Tomcy John


Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?
Please provide an example for StoredProcedureItem Reader – which returns cursor and how to process cursor in processor – I am new to Spring Batch as part of my work I need to invoke Oracle Stored proc which takes one param as input and returns result set which I need to process in Spring Batch Processor.
Thanks
Laxmi