0
votes

I am looking for an implementation in Spring Batch which would execute READ -> PROCESS -> WRITE for flat files.

We would be reading say 10 files from an input folder and writing 10 files in the output folder. For each input file there will be some processing/transformations on the data and correspondingly an output (transformed) file would be generated.

Please suggest/guide on how to go about it.

2

2 Answers

1
votes

You can use MultiResourceItemReader.

See following sample code

<bean id="multiResourceItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
        <property name="resources"
 value="file:./src/main/resources/input/*.txt" />       <property
 name="delegate">           <bean
 class="org.springframework.batch.item.file.FlatFileItemReader">
                <property name="lineMapper">
                    <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                        <property name="lineTokenizer">
                            <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                                <property name="names" value="firstname,lastname,birth" />
                            </bean>
                        </property>
                        <property name="fieldSetMapper">
                            <bean class="com.zenika.workshop.springbatch.ContactFieldSetMapper" />
                        </property>
                    </bean>
                </property>             </bean>         </property>     </bean>

Here the MultiResourceItemReader is reading all .txt file from /src/main/resources/input folder.

0
votes

simple clue

<bean   class="CustomReader1"   id="cr1">
    <property   name="folderLocation" value="/path/to/sourcefiles/parent/directory/"/>
</bean>
<bean   class="CustomReader2"   id="cr2">
    <property   name="filesLocations">
        <list>
            <value>/path/to/sourcefiles/parent/directory/file1.txt</value>
            <value>/path/to/sourcefiles/parent/directory/file2.txt</value>
            <value>/path/to/sourcefiles/parent/directory1/file1.txt</value>
            <value>/path/to/sourcefiles/parent2/directory/file1.txt</value>
        </list>
    </property>
</bean>
<bean   class="MainItemReader"  id="mainItemReader">
    <property   name="itemReaders">
        <list>
            <bean   ref="cr1"/>
            <bean   ref="cr2"/>
        </list>
    </property>
</bean>