Below is the code for a customized flat file item reader that reads multiple items
public class MultipleItemsFlatFileReader<T> implements ItemReader<List<T>>, ItemStream {
private FlatFileItemReader<T> reader;
private int fetchSize;
public void setReader(String readerName, String filePath, String[] headers, int[] includedFileds,
Class<? extends T> c, int fetchSize) {
this.reader = CustomFlatFileReader.getReader(readerName, filePath, headers, includedFileds, c);
this.fetchSize = fetchSize;
}
@Override
public List<T> read() throws Exception{
List<T> items = new ArrayList<>();
for (int count = 0; count < this.fetchSize; count++) {
T item = reader.read();
if (item == null) {
break;
}
items.add(item);
}
if (!items.isEmpty()) {
return items;
}
return null;
}
@Override
public void open(ExecutionContext executionContext) {
reader.open(executionContext);
}
@Override
public void update(ExecutionContext executionContext) {
reader.update(executionContext);
}
@Override
public void close() {
reader.close();
}
}
Below is the code for custom item writer
public class MultipleItemsCompositeJdbcWriter<T> implements ItemWriter<List<T>> {
private List<JdbcBatchItemWriter<T>> delegates;
public void setDelegates(List<JdbcBatchItemWriter<T>> writers) {
this.delegates = writers;
}
@Override
public void write(List<? extends List<T>> items) throws Exception {
for (JdbcBatchItemWriter<T> writer: delegates) {
for (List<T> item: items) {
writer.write(item);
}
}
}
}
I am new to spring batch. Is this code correct? Any use case I might be missing? Currently my batch job executes sequentially but may be in future it could use multi threading and partitioning.
Need for this is that I need to do data base look ups in the processor. Doing the look ups for multiple items is better than doing it for a single item.