abhishek
abhishek

Reputation: 1

How to send the data of 2 jdbc readers to one flat file using Spring Batch

I need to write a job in which i have to read the database 2 times.

First query returns only one record and i need to send this record as the header of my flat file.
Second query returns a set of records that i need to write in same flat file.

I tried to create a compositewriter for it and within that i provided two flatFileItemWriter and set same resource name for both the flatFileItemWriters. But this is resulting in a error that resource alread exists.

Can anyone tell me the approach that i should follow.

Thanks in advance.

Upvotes: 0

Views: 632

Answers (2)

Serkan Arıkuşu
Serkan Arıkuşu

Reputation: 5619

Write your bean taking into account of your second query which returns a set of records. For the header part, you give a FlatFileHeaderCallback as a property to your FlatFileItemWriter and get the header record there.

public class MyFileHeaderCallback implements FlatFileHeaderCallback {
    @Override
    public void writeHeader(Writer writer) throws IOException {
        writer.write("#I have this line from a query");
    }
}

<bean id="headerCallback" class="com.file.MyFileHeaderCallback" />

<bean id="productItemWriter" class="org.*.file.FlatFileItemWriter">
<property name="headerCallback" ref="headerCallback" />
<!-- other necessary methods for your Writer -->
</bean>

Upvotes: 0

abalogh
abalogh

Reputation: 8281

Write into two different files with FlatFileItemWriter and in a 3rd step append the files.

Upvotes: 1

Related Questions