1

我试图将 xml 文件解析为 flatefile。除了最后,一切都很好。在调试模式下,无限循环的原因是 org.springframework.batch.item.xml.StaxEventItemRe ader.class 中的 moveCursorToNextFragment(reader) 方法。

在这部分方法中更精确

while (reader.peek() != null && !reader.peek().isStartElement()) {
    reader.nextEvent();
}

似乎这些条件不足以识别文件的结尾。我用这段代码替换这部分代码以确保文档测试结束

while (reader.peek() != null && !reader.peek().isStartElement() &&    !reader.peek().isEndDocument()) {
    reader.nextEvent();
}
if (reader.peek() == null || reader.peek().isEndDocument()) {
 return false;
}

在我的上下文文件和 INSEE xmlfile 的链接下方:http://lei-france.insee.fr/Telechargement.do 总是拿完整的文件。

这是我的上下文文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"   
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/batch
                    http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
                    http://www.springframework.org/schema/jee
                    http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
  p:dataSource-ref="dataSource"/>

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
  p:jobRepository-ref="jobRepository" 
  p:taskExecutor-ref="taskExecutor" /> 

<bean id="jobRepository"  class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
  p:dataSource-ref="dataSource"
  p:transactionManager-ref="transactionManager" />

<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"/> 

<!-- DATASOURCE -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    <property name="driverClassName"
              value="oracle.jdbc.driver.OracleDriver" />        
<property name="url"
              value="jdbc:oracle:thin:@gulli:1521:OPE" />
    <property name="username" value="xxxxxx" />
    <property name="password" value="xxx" />
</bean> 

<!-- ITEM READER -->
<bean id="itemReader" scope="step" class="org.springframework.batch.item.xml.StaxEventItemReader"
p:resource="insee/lei.xml"
p:fragmentRootElementName="LEIRecord"
p:unmarshaller-ref="inseeMarshaller" />

<bean id="inseeMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"
p:autodetectAnnotations="true" p:encoding="UTF-8">
<property name="aliases">
 <props>
  <prop key="LEIDirectory">fr.cdn.dtc.etudes.emir.insee.LEIDirectory</prop>
  <prop key="LEIRecord">fr.cdn.dtc.etudes.emir.insee.LEIRecord</prop>
  <prop key="LegalEntity">fr.cdn.dtc.etudes.emir.insee.LegalEntity</prop>
  <prop key="OtherIdentifiers">fr.cdn.dtc.etudes.emir.insee.OtherIdentifiers</prop>
  <prop key="OtherNames">fr.cdn.dtc.etudes.emir.insee.OtherNames</prop>
  <prop key="RelatedLEI">fr.cdn.dtc.etudes.emir.insee.RelatedLEI</prop>
  <prop key="Event">fr.cdn.dtc.etudes.emir.insee.Event</prop>
  <prop key="OtherAddresses">fr.cdn.dtc.etudes.emir.insee.OtherAddresses</prop>
 </props>
</property>
</bean> 

<!-- ITEM WRITER DELIMETED LENGTH--> 
<bean id="itemWriterFile" class="org.springframework.batch.item.file.FlatFileItemWriter"
p:resource-ref="outputDelimitedResource" 
p:lineAggregator-ref="lineAggregator"
p:shouldDeleteIfExists="true" 
/>

<bean id="outputDelimitedResource" class="org.springframework.core.io.FileSystemResource"> 
<constructor-arg value="target/outputs/inseeOutput.txt" />
</bean>

<bean id="lineAggregator" class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"
p:delimiter=";"
p:fieldExtractor-ref="fieldExtractor" />

<bean id="fieldExtractor" class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"
p:names="le" />

<batch:job id="inseeJob" job-repository="jobRepository" >
 <batch:step id="inseeStep"> 
  <batch:tasklet>
   <batch:chunk reader="itemReader" writer="itemWriterFile" commit-interval="10" />
   <batch:transaction-attributes isolation="DEFAULT" propagation="REQUIRED" timeout="30"/>  
  </batch:tasklet>  
 </batch:step>
</batch:job>

</beans>        

最好的问候克莱门特

4

0 回答 0