Java Reference
In-Depth Information
delegate.open(executionContext);
}
@Override
public void update(ExecutionContext executionContext)
throws ItemStreamException {
executionContext.put("records.processed", itemsProcessedSoFar);
delegate.update(executionContext);
}
@Override
public void setResource(Resource arg0) {
itemsProcessedSoFar = 0;
delegate.setResource(arg0);
}
}
As you can see in Listing 9-72 when a resource is set on the ItemWriter, the counter
(
itemsProcessedSoFar
) is set to 0. As items are written via the
write
method, the counter is incremented
accordingly. When the
writeFooter
method is called, the counter is used in the footer output to list the
number of records that are in the file that is being closed.
There are three other methods to take note of in this implementation:
open
,
update
, and
close
. The
open
and
update
methods end up being more than just passthroughs to make this ItemWriter restartable.
Because the ItemWriter has its own state (the number of records that have been processed in the current
file), you want to save that in the
ExecutionContext
in case the job fails. The
update
method is used to
save that value during processing. The
open
method is used to reset where you left off in the event the job
is restarted. The
close
method serves as only a passthrough to the delegate so that it can close the file as
required.
To configure the various ItemWriters required for this example, Listing 9-73 shows the required
XML. You begin with the input, reading the customer records out of the Customer table. From there, you
configure the three ItemWriter implementations. The first is
FlatFileItemWriter
and its required
LineAggregator
. From there, you configure
customerWriter
, which is the implementation of the
ItemWriter (
ResourceAwareItemReaderItemStream
is a sub-interface of the ItemReader interface) in
Listing 9-72. Finally, you have the
multiResourceItemWriter
you've used previously. Listing 9-73 finishes
with the configuration of the step and job.
Listing 9-73.
customerFooterFormatJob.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="
http://www.springframework.org/schema/batch"
xmlns:beans="
http://www.springframework.org/schema/beans"
xmlns:util="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
<beans:import resource="../launch-context.xml"/>