Spring Book – Chapter 21 – Spring Batch

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.


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

Figure 23-15. Working of RepeatTemplate with RepeatCallback


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


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


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

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)

Listing 23-28. Restart configuration in Spring configuration file in which you can limit the number of step restart allowed (default is unlimited)

Page Visitors: 25402

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 21 – Spring Batch

  1. Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?

  2. 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.


Leave a Reply

Your email address will not be published. Required fields are marked *