[英]Spring Batch: Create an ItemReader that reads an xml file from a web service
[英]Spring Batch: MultiResourceItemReader with custom ItemReader reads the same file ad infinitum
我有一個帶有自定義ItemReader的MultiResourceItemReader作為委托。 我面臨的問題是,當我啟動作業時,一次又一次讀取同一文件。
這是委托類:
public class AllegatiReader implements ResourceAwareItemReaderItemStream<Allegato> {
@PersistenceContext
protected EntityManager em;
private Resource resource;
@Override
public void close() throws ItemStreamException {
}
@Override
public void open(ExecutionContext arg0) throws ItemStreamException {
}
@Override
public void update(ExecutionContext arg0) throws ItemStreamException {
}
@Override
public Allegato read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
// DO SOMETHING ...
byte[] fileContent = new byte[(int) resource.getFile().length()];
resource.getInputStream().read(fileContent);
resource.getInputStream().close();
allegato.getFile().setFile(fileContent);
return allegato;
}
@Override
public void setResource(Resource arg0) {
this.resource = arg0;
}
}
這是我的Spring Batch XML配置文件:
<batch:job id="allegati" incrementer="jobParametersIncrementer">
<batch:step id="allegati-import">
<batch:tasklet>
<batch:chunk reader="allegati-reader" writer="allegati-writer" commit-interval="1"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="allegati-reader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
<property name="resources" value="file:#{jobParameters['FILEPATH']}/*" />
<property name="delegate" ref="allegati-filereader" />
</bean>
<bean id="allegati-writer" class="org.springframework.batch.item.database.JpaItemWriter">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="allegati-filereader" class="it.infogroup.vertenze.porting.reader.AllegatiReader" />
如何告訴Spring Batch移至下一個文件?
完成后,您的自定義閱讀器必須顯示Spring Batch,請參閱http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/ItemReader.html#read--
讀取一條輸入數據並前進到下一個。 實現必須在輸入數據集的末尾返回null。
在您的情況下,我將使用私有屬性保存* 處理此閱讀器實例的資源的狀態,它可能是Allegato對象,但這似乎是一個很大的對象
*)您的讀者在設計上是有狀態的,因此另一個狀態屬性應該沒問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.