Can you configure an ItemReader for a Partitioner in Spring Batch?

1k Views Asked by At

I have the following requirement: a CSV input file contains lines where one of the fields is an ID. There can be several lines with the same ID. The lines should be processed grouped by ID (meaning, if one line fails validation, then all lines with that same ID should fail to process). The groups of lines can be processed in parallel.

I have an implementation that works fine, but it is reading the CSV input file using my own code in a Partitioner implementation. It would be nicer if I could use an out-of-the-box implementation for that (e.g. FlatFileItemReader) and just configure that just like you would for a Chunk step.

To clarify, my job config is like this:

<batch:job id="job">
    <batch:step id="partitionStep">
        <batch:partition step="chunkStep" partitioner="partitioner">
            <batch:handler grid-size="10" task-executor="taskExecutor" />
        </batch:partition>      
    </batch:step>       
</batch:job>

<batch:step id="chunkStep">
    <batch:tasklet transaction-manager="transactionManager">
        <batch:chunk reader="reader" processor="processor" writer="writer" chunk-completion-policy="completionPolicy">
            .. skip and retry policies omitted for brevity              
        </batch:chunk>              
    </batch:tasklet>
</batch:step>

<bean id="partitioner" class="com.acme.InputFilePartitioner" scope="step">
    <property name="inputFileName" value="src/main/resources/input/example.csv" />
</bean>

<bean id="reader" class="org.springframework.batch.item.support.ListItemReader" scope="step">
    <constructor-arg value="#{stepExecutionContext['key']}"/>   
</bean> 

where the Partitioner implementation reads the input file, "manually" parses the lines to get the ID field, groups them by that ID and puts them in Lists, and create ExecutionContexts that each get one of those Lists.

It would be great if I could replace that "manual" code in the Partitioner by a configuration of FlatFileItemReader with an ObjectMapper. (I hope I express myself clearly).

Is it possible ?

0

There are 0 best solutions below