我需要从文件夹中读取csv文件并写入DB,同时归档文件。以下是xml
中描述的工作<job id="importJob" xmlns="http://www.springframework.org/schema/batch">
<step id="validateFileStep" >
<tasklet ref="validateFileTasklet" />
<next on="*" to="importFileStep" />
<next on="FAILED" to="archiveFileStep" />
</step>
<step id="importFileStep" next="archiveFileStep">
<tasklet>
<chunk reader="multiResourceReader" writer="csvWriter"
commit-interval="1" >
</chunk>
</tasklet>
</step>
<step id="archiveFileStep">
<tasklet ref="archiveFileTasklet" />
</step>
</job>
multiResourceReader bean定义
<bean id="multiResourceReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
<property name="resources" value="classpath:data/*.csv" />
<property name="delegate" ref="csvReader" />
</bean>
代码工作正常,但是我没有在xml中提供“resources”属性值,而是需要从“validateFileStep”步骤获取它,因为读者应该只读取那些格式正确的csv文件。我也应该将它传递给archiveFileStep,这样文件的名称就可以在存档中附加完成或失败。
下面是reader bean def
<!-- property to skip the headers, as no headers in CSV, value is 0 -->
<property name="linesToSkip" value="0" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value="," />
<!-- Names of the CSV columns -->
<property name="names"
value="id,name,city" />
</bean>
</property>
<property name="fieldSetMapper">
<bean
class="com.common.service.csvFieldSetMapper" />
</property>
</bean>
</property>
</bean>