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

标签 java spring-batch

我有一个 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/

相关文章:

spring - 如何在没有 itemwriter 的情况下编写 spring 批处理步骤

java - Spring Batch CommandLineJobRunner -next 使用旧的 run.id

java - Spring Batch JdbcPagingItemReader 分页不起作用

java - 用尾随零填充 DateTimeFormatter

Java 错误 - "invalid method declaration; return type required"

java - Android内存管理从 native JNI调用Java类并声明数据(用于图像转换)

java - 如何通过java socket编程开发客户端-服务器订座程序?

java - Spring Batch 有哪些替代方案来处理排队的作业?

java - FF 为我的 tomcat HTTPS 抛出 Strict-Transport-Security

java - Spring batch writer节流