ItemProcessor
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
1 2 3 4 5 6 7 |
package org.springframework.batch.item; public interface ItemProcessor<I, O> { O process(I item) throws Exception; } |
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
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
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:batch="http://www.springframework.org/schema/batch" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd"> … </beans> |
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
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