Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

java - How to set the resource of MultiResourceItemWriter dynamically from ItemReader in Spring Batch

The batch job is:

  • Reading from a csv file

  • Create an xml file for every record(line) in the csv with name Patent.ID.xml(where ID is a field in the csv and Patent is my model class), example: 1.xml, 2.xml

The problem is that I can't find a way to dynamically set the file-name to each ID from the csv file

Here is my configuration:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch"xmlns:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/batch
        http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.2.xsd">


    <!-- My beans -->   
    <bean id="patent" class="com.example.model.Patent" scope="prototype" />
    <bean id="xmlsuffix" class="com.example.filename.PatentFileSuffixCreator"/>
    <bean id="noInputException"     class="com.example.listener.NoWorkFoundStepExecutionListener"/>

    <batch:job id="csvtoxml">
        <batch:step id="step1">
            <batch:tasklet>             
                <batch:chunk reader="fileReader"     writer="multiResourceItemWriter" commit-interval="1">
                </batch:chunk>
                <batch:listeners>
                    <batch:listener ref="noInputException"/>
                </batch:listeners>  
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="multiResourceItemWriter"
    class="org.springframework.batch.item.file.MultiResourceItemWriter">
        <property name="resource" value="file:xml/#{patent.ID}" />
        <property name="delegate" ref="XMLwriter"/>
        <property name="itemCountLimitPerResource" value="1"/>
        <property name="resourceSuffixCreator" ref="xmlsuffix"/>
    </bean>

    <bean id="XMLwriter" 
                class="org.springframework.batch.item.xml.StaxEventItemWriter">
        <property name="marshaller" ref="patentUnmarshaller" />
        <property name="rootTagName" value="Patents" />
    </bean>

    <bean id="patentUnmarshaller" 
     class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="aliases">
            <map>
                <entry key="Patent"
                value="com.example.model.Patent" />
            </map>
        </property>
    </bean>

  <bean id="fileReader"
        class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <property name="lineMapper" ref="lineMapper"/>
        <property name="resource" value="file:dbbrev_sample_m.csv"/>
        <property name="linesToSkip" value="1"/>
    </bean>

    <bean id="lineMapper"
        class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
        <property name="fieldSetMapper" ref="fieldSetMapper"/>
        <property name="lineTokenizer" ref="lineTokenizer"/>
    </bean>

    <bean id="lineTokenizer"
        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
        <property name="delimiter" value=","/>
        <property name="names" value="ID,TYPE,NOPUBLICATION" />
    </bean>

    <bean id="fieldSetMapper"
        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
        <property name="targetType" value="com.example.model.Patent"/>
    </bean>
</beans>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create a ItemWriteListener, inject the multiResourceItemWriter bean and bound the listener to your step.
In ItemWriteListener.beforeWrite() create a new ResourceSuffixCreator object using the item your are going to write as base to create resource extension (suffix).
MultiResourceItemWriter.resource probably need to be changed as file:xml/ because 1.xml, 2.xml and so on will be appended using custom ResourceSuffixCreator dinamically created for every item you are writing.

This solution is dirty and (probably) works due to commit-interval=1; if you change my answer (probably) will fail.

I hope I was clear, English is not my native language.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...