Java Reference
In-Depth Information
C H A P T E R 7
The three R's, Reading, wRiting and aRithmetic, are considered the basis of the skills children learn in
schools. When you think about it, these same concepts apply to software as well. The foundations of any
programs—whether web applications, batch jobs, or anything else—are the input of data, the processing
of it in some way, and the output of data.
This concept is no more obvious than when you use Spring Batch. Each step consists of an
ItemReader, an ItemProcessor, and an ItemWriter. Reading in any system isn't always straight forward,
however. There are a number of different formats in which input can be provided; flat files, XML, and
databases are just some of the potential input sources.
Spring Batch provides standard ways to handle most forms of input without the need to write code
as well as the ability to develop your own readers for formats that are not supported, like reading a web
service. This chapter will walk through the different features ItemReaders provide within the Spring
Batch framework.
The ItemReader Interface
Up to this chapter we have vaguly discussed the concept of an ItemReader but we have not looked at the
interface that Spring Batch uses to define input operations. The
org.springframework.batch.item.ItemReader<T> interface defines a single method, read that is used to
provide input for a step. Listing 7-1 shows the ItemReader interface.
Listing 7-1. org.springframework.batch.item.ItemReader<T>
package org.springframework.batch.item;
public interface ItemReader<T> {
T read() throws Exception, UnexpectedInputException, ParseException,
The ItemReader interface shown in Listing 7-1 is a strategy interface. Spring Batch provides a
numer of implementations based on the type of input to be processed. Flat files, databases, JMS
resources and other sources of input all have implementations provided by Spring Batch. You can also
implement your own ItemReader by implementing the ItemReader or any one of its subinterfaces.
The read method of the ItemReader interface returns a single item to be processed by your step as it
is called by Spring Batch. This item is what your step will count as it maintains how many items within a
Search WWH ::

Custom Search