Saturday, October 29, 2011

Reading data from source like flat files and databases are common theme in batch projects. Spring Batch provides a lot of support for reading from these data sources out of the box. The FlatFileItemReader<T> provides a great example. This class extends the abstract AbstractItemCountingItemStreamItemReader<T>, which for one reason or another, I wanted to implement myself for reading a flat file. Extensions of this inteface must implement three methods:

doClose()

doOpen()

doRead()

Since I was implementing the interface myself, I had to come up with a way to actually read the contents of the file myself. In the Google Guava project, I found a class to use, LineReader, that seemed like a good candidate to help alleviate the need for the usual tedious boilerplate code required for reading the contents of the file.

Before describing the use of the LineReader in a Spring Batch project, I wanted to demonstrate an example of using the LineReader outside of the context of a Spring Batch project. The Maven coordinates for the current version of Guava are:

groupId: com.google.guava

artifactId: guava

version: 10.0.1

Below is a sample class that follows the same pattern that must be implemenented when extending the AbstractItemCountingItemStreamItemReader class using a LineReader. The class takes the name of a resource to read from and handles making the resource available to the LineReader in the doOpen() method. Reading from the resource and closing of the resource are handled by the doRead() and doClose() methods respectively.

To test the above class, we'll use a file, tests.txt that contains three lines:

line1 line2 line3

As the doRead() method, by way of LineReader.readLine(), will return null when the data from the input resource has been exhausted, we can use that fact to know when to call the doClose() method. At the end of our test we have a check to verify that three lines were read from the file:

In the above configuration, the scope of step ensures the bean is reconfigured every time the reader is used within a job. Spring Expression Language is used to inject the the name of the file resource into the reader from the job's parameters.