0
votes

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.

1
Welcome to SO. I added an answer, please accept it if it helped: stackoverflow.com/help/someone-answers. Thank you.Mahmoud Ben Hassine

1 Answers

0
votes

Is this code correct?

The answer depends on the specification of your job. It looks like an item in your case is a logical group of multiple physical lines in a flat file.

For the reader, the following section from the documentation can help: https://docs.spring.io/spring-batch/4.0.x/reference/html/common-patterns.html#multiLineRecords

There are also two samples for this use case:

For the writer, you can use the CompositeItemWriter to write items with multiple writers. More details here: https://docs.spring.io/spring-batch/4.0.x/api/org/springframework/batch/item/support/CompositeItemWriter.html