The batch job is:
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…