Java Reference
In-Depth Information
Listing 9-72. CustomerWriterWithFooter
package com.apress.springbatch.chapter9;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.file.FlatFileFooterCallback;
import org.springframework.batch.item.file.ResourceAwareItemWriterItemStream;
import org.springframework.core.io.Resource;
public class CustomerWriterWithFooter implements
ResourceAwareItemWriterItemStream<Customer>, FlatFileFooterCallback {
private ResourceAwareItemWriterItemStream<Customer> delegate;
private int itemsProcessedSoFar = 0;
@Override
public void writeFooter(Writer writer) throws IOException {
writer.write("At the end of this file, you have written " +
itemsProcessedSoFar + " items");
}
@Override
public void write(List<? extends Customer> items) throws Exception {
itemsProcessedSoFar += items.size();
delegate.write(items);
}
public void setDelegate(
ResourceAwareItemWriterItemStream<Customer> delegate) {
this.delegate = delegate;
}
@Override
public void close() throws ItemStreamException {
delegate.close();
}
@Override
public void open(ExecutionContext executionContext)
throws ItemStreamException {
if(executionContext.containsKey(“records.processed”)) {
itemsProcessedSoFar = Integer.parseInt(executionContext
.get(“records.processed”).toString());
}
Search WWH ::




Custom Search