[英]Spring Batch: MultiResourceItemReader with custom ItemReader reads the same file ad infinitum
I have a MultiResourceItemReader with a custom ItemReader as a delegate. 我有一个带有自定义ItemReader的MultiResourceItemReader作为委托。 The problem I'm facing is that when I launch the job, the same file is read over and over again. 我面临的问题是,当我启动作业时,一次又一次读取同一文件。
This is the delegate class: 这是委托类:
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;
}
}
Here is my Spring Batch XML configuration file: 这是我的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" />
How can I tell Spring Batch to move to the next file? 如何告诉Spring Batch移至下一个文件?
Your custom reader has to show Spring Batch when all is done, see http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/ItemReader.html#read-- 完成后,您的自定义阅读器必须显示Spring Batch,请参阅http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/ItemReader.html#read--
Reads a piece of input data and advance to the next one. 读取一条输入数据并前进到下一个。 Implementations must return null at the end of the input data set. 实现必须在输入数据集的末尾返回null。
in your case i would use an private attribute to save* the state for the resource of this reader instance is processed , it could be the Allegato object, but that seems to be a rather large one 在您的情况下,我将使用私有属性保存* 处理此阅读器实例的资源的状态,它可能是Allegato对象,但这似乎是一个很大的对象
*) your reader is stateful by design, so another state attribute should be no problem *)您的读者在设计上是有状态的,因此另一个状态属性应该没问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.