Spring Batch - 读取多个PDF文件并将它们传递给ItemProcessor

时间:2014-12-17 15:43:33

标签: spring spring-batch

我想阅读多个pdf文件并逐个处理。

我使用MultiResourceItemReader和自定义委托:

public class MyItemReader implements ResourceAwareItemReaderItemStream<MyItem> {

    private Resource resource;

    @Override
    public MyItem read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        return null; //create MyItem
    }

    @Override
    public void setResource(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void open(ExecutionContext executionContext) throws ItemStreamException {
    }

    @Override
    public void update(ExecutionContext executionContext) throws ItemStreamException {
    }

    @Override
    public void close() throws ItemStreamException {
    }
}

我遇到的问题是无限期地调用了read方法,并且没有调用我的ItemProcessor。 资源属性已正确设置 - 已设置文件。 谁能解释一下这个?提前谢谢。

我最终决定使用ResourcesItemReader而不是MultiResourceItemReader和自定义委托。这个解决方案更简单。

<!--suppress SpringBatchModel -->
<batch:job id="my-import">
    <batch:step id="myFileStep">
        <batch:tasklet>
            <batch:chunk reader="resourcesItemReader"
                         processor="sddeImportProcessor"
                         writer="sddeImportJpaItemWriter"
                         commit-interval="${commit.interval:500}"/>
        </batch:tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="sftpImportExecutionListener"/>
        <batch:listener ref="longRunningJobExecutionNotificator"/>
        <batch:listener ref="exitStatusJobExecutionListener"/>
        <batch:listener ref="afterJobExecutionMailSender"/>
    </batch:listeners>
</batch:job>

<bean id="sftpImportExecutionListener"
      class="my.batches.shared.listener.SftpImportJobListener">
    <constructor-arg name="ftsReadService" ref="ftsReadService"/>
    <constructor-arg name="ftsWriterService" ref="ftsWriterService"/>
    <constructor-arg name="localDir" value="${voe.batch.sdde.unterschriftenblatt.import.local.folder}"/>
    <constructor-arg name="remoteDir" value="${voe.batch.sdde.unterschriftenblatt.import.remote.folder}"/>
    <constructor-arg name="multipleFilesImport" value="true" />
</bean>

<bean id="resourcesItemReader" class="org.springframework.batch.item.file.ResourcesItemReader" scope="step">
    <property name="resources" value="#{jobExecutionContext['import.input.file.path']}"/>
</bean>

<bean id="myImportProcessor" class="my.MyProcessor">
    <property name="myUpdateService" ref="defaultUpdateService" />
</bean>

<bean id="myImportJpaItemWriter" class="org.springframework.batch.item.database.JpaItemWriter">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

0 个答案:

没有答案