Java Reference
In-Depth Information
The reasoning is that Spring Batch is counting the number of items that were written. It doesn't care
how many places you write the item to. If the job fails, the restart point depends on how many items you
read and processed, not how many you wrote to each location (because those are rolled back anyway).
The
CompositeItemWriter
makes writing all the items to multiple locations easy. But sometimes you
want to write some things to one place and some things to another place. The last ItemWriter you look at
in this chapter is
ClassifierCompositeItemWriter
, which handles just that.
ClassifierCompositeItemWriter
In Chapter 7, you looked at the scenario where you had a single file that contained multiple record types.
Handling the ability to map different types of lines to different parsers and mappers so that each would
end up in the correct object was no trivial task. But on the writing side, Spring Batch has made life a bit
easier. This section looks at how
ClassifierCompositeItemWriter
allows you to choose where to write
items based on a predetermined criteria.
org.springframework.batch.item.support.ClassifierCompositeItemWriter
is used to look at items
of different types, determine what ItemWriter they should be written to, and forward them accordingly.
This functionality is based on two things:
ClassifierCompositeItemWriter
and an implementation of the
org.springframework.batch.classify.Classifier
interface. Let's start by looking at the
Classifier
interface.
The
Classifier
interface, shown in Listing 9-76, consists of a single method,
classify
. In the case of
what
ClassifierCompositeItemWriter
uses a
Classifier
implementation for, the
classify
method
accepts an item as input and returns the ItemWriter to write the item to. In essence, the
Classifier
implementation serves as a context, with the ItemWriters as strategy implementations.
Listing 9-76.
The
Classifier
Interface
package org.springframework.batch.classify;
public interface Classifier<C, T> {
T classify(C classifiable);
}
ClassifierCompositeItemWriter
takes a single dependency, an implementation of the
Classifier
interface. From there it gets the ItemWriter required for each item as it's processed.
Unlike the regular
CompositeItemWriter
, which writes all items to all ItemWriters,
ClassifierCompositeItemWriter
ends up with a different number of items written to each ItemWriter.
Let's look at an example where you write all customers who live in a state that starts with the letters
A
through
M
to a flat file and items with a state name starting with the letters
N
through
Z
to the database.
As you've probably gathered, the
Classifier
implementation is the key to making
CompositeItemWriter
work, so that is where you start. To implement this
Classifier
as Listing 9-77
shows, you take a
Customer
object as the sole parameter to the
classify
method. From there, you use a
regular expression to determine whether it should be written to a flat file or the database and return the
ItemWriter as required.
Listing 9-77.
CustomerClassifier
package com.apress.springbatch.chapter9;
import org.springframework.batch.classify.Classifier;