Spring Book – Chapter 21 – Spring Batch


ItemProcessor is an abstraction that represents the business processing of an item. While the ItemReader reads one item, and the ItemWriter writes them, if you want to ‘transform’ the item passed in for writing before it is actually written, Spring Batch provides the ItemProcessor interface as shown in Listing 23-6 below.

Listing 23-6. ItemProcessor interface definition

Given an item as input, this interface provides an extension point which allows for the application of business logic in an item oriented processing scenario. It should be noted that while it’s possible to return a different type than the one provided, it’s not strictly necessary. Furthermore, returning null indicates that the item should not be continued to be processed.


ExecutionContext in simple terms is a key/value pairs persisted by Spring Batch. This is used by the framework to maintain state of the whole execution. In the instance of a restart, this state information is used by the framework to locate where the failure occurred and restarts the job accordingly. There are two flavors of ExecutionContext:

  • Job ExecutionContext – Committed at the end of each step and can be used to pass state between two steps in a job. The class JobExecution resides in package org.springframework.batch.core.
  • Step ExecutionContext – committed at the end of each chunk. The class StepExecution resides in package org.springframework.batch.core.

It is important to note that there is at least one ExecutionContext per JobExecution, and one for every StepExecution.

ItemReader, ItemWriter and ExecutionContext

Most of the reader’s and writer’s provided by Spring Batch is stateful in nature and to achieve it they have reference to ExecutionContext which in turn stores its state in the batch meta-data tables in the database as shown in Figure 23-8 below.

 Figure 23-8. ItemReader, ItemWriter and ExecutionContext

Figure 23-8. ItemReader, ItemWriter and ExecutionContext

We will look at the various off-the-shelf ItemReader’s and ItemWriter’s provided in Spring Batch in later sections of this same Chapter.

Spring Batch Namespace

To configure Spring Batch related configurations easily in the Spring configuration file, it provides appropriate Spring Batch namespace, using which the configuration can be made easily and in a concise manner. To start using the namespace in your Spring configuration file, you need to declare these appropriately as shown in Listing 23-7 below.

Listing 23-7. Configuring Spring Batch Namespace

ItemReaders and ItemWriters

Spring Batch provides many off-the-shelf implementations of ItemReader’s and ItemWriter’s. Common business uses cases can be implemented with off-the-shelf components which the Spring Batch provides. The off-the-shelf component which Spring Batch already has is as summarized below:

  • Flat files
  • XML
  • Database (JDBC)
  • Hibernate
  • JPA
  • JMS

In the following sections we will cover some of the important ItemReader’s and ItemWriter’s. Finally we will summarize all the reader’s and writer’s in available Spring Batch for your reference and easy reading. Spring Batch interacts with external world as depicted pictorially in Figure 23-9 below.

Figure 23-9. Interaction of Spring Batch with external world

Figure 23-9. Interaction of Spring Batch with external world

Page Visitors: 26000

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 *