0
votes

In spring batch, I have a requirement where i have list of customer objects from database and from this list i need to create multiple text files.

clas Customer{
    long customerId;
    string name;
    Address add;
    Phone phn;
}

class Address{
    string address;
    long pincode;
    string street;
}

class Phone{
    string phoneNumber;
    string desc;
}

I want to write the data of each customer to different text files such as

customer.txt -> customerId, name
address.txt -> address, pincode, street
phone.txt -> phoneNumber, desc

i tried using CompositeItemWriter which can delegate object to different writer but i could not get how to retrive specific object or properties in the writer.

I couldnt get any sample or explaination to implement CompositeWriter. can someone please help me with the writer implementation? Is there any other better way of doing it?

Thanks in advance.

1
Check for classifier writer - Luca Basso Ricci

1 Answers

1
votes

I was able to write data of each customer to different text file using CompositeItemWriter. Below is how i did for customer phone extract. I hope it helps someone.

<bean id="compositeCustomerWriter"
        class="org.springframework.batch.item.support.CompositeItemWriter">
        <property name="delegates">
            <list>
                <ref bean="customerWriter" />
                <ref bean="customerPhoneWriter" />
                <ref bean="customerAddressWriter" />
            </list>
        </property>
</bean>

<bean id="customerPhoneWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
        <property name="resource" value="file:customer_phone_extract.txt" />
        <property name="lineAggregator" ref="customerPhonelineAggregator" />
</bean>

<bean id="customerPhonelineAggregator"
    class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
        <property name="delimiter" value="|" />
        <property name="fieldExtractor">
            <bean
                class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                <property name="names"
                    value="phn.phoneNumber,phn.desc" />
            </bean>
        </property>
</bean>