gpt4 book ai didi

java - Spring 批处理 : MultiResourceItemReader with custom ItemReader reads the same file ad infinitum

转载 作者:行者123 更新时间:2023-12-02 04:49:22 25 4
gpt4 key购买 nike

我有一个 MultiResourceItemReader,其中有一个自定义 ItemReader 作为委托(delegate)。我面临的问题是,当我启 Action 业时,会一遍又一遍地读取同一个文件。

这是委托(delegate)类:

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--

Reads a piece of input data and advance to the next one. Implementations must return null at the end of the input data set.

在你的情况下,我将使用一个私有(private)属性来保存*状态该读取器实例的资源已处理,它可能是 Allegato 对象,但这似乎是一个相当大的对象

*) 您的阅读器在设计上是有状态的,因此另一个状态属性应该没有问题

关于java - Spring 批处理 : MultiResourceItemReader with custom ItemReader reads the same file ad infinitum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29374155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com