开发者

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

开发者 https://www.devze.com 2023-01-15 17:16 出处:网络
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.

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 开发者_如何学JAVAi should follow.

Thanks in advance.


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


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消