处理后从目录中删除flatfile

处理后从目录中删除flatfile

本文介绍了Spring批处理 - 处理后从目录中删除flatfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在spring批处理中,我使用MultiResourceItemReader从目录中读取多个文件。然后我使用FlatFileItemReader作为委托处理单个文件。我的用例是在文件完全处理后删除文件(完成READ-WRITE),然后multiResourceitemReader必须选择另一个文件,它必须继续。

In spring batch , I am using MultiResourceItemReader to read multiple files from the directory. Then I am using a FlatFileItemReader as a delegate to process individual files. My usecase is to delete the file once it is processed completely(READ-WRITE is done) and then multiResourceitemReader has to pick another file and it has to go on.

I尝试使用FileDeletingTasklet删除目录中的文件,但是根据Spring文档,execute方法只会被调用一次。如何在文件上删除已处理的文件(READ-WRITE),但是我不希望在目录中完全处理完所有文件后再删除整个目录。

I tried FileDeletingTasklet to delete file in a directory, but as per Spring docs , the execute method will be called only once. How can I achieve delete on file which are processed(READ-WRITE), but I don't want to go with entire directory delete once all files are processed completely in the directory.

以下是我正在使用的工作:

Below is the job I am using :

<batch:job id="getEmpDetails">
    <batch:step id="readAndProcess" next="deleteProcessedFile">
        <batch:tasklet>
            <batch:chunk reader="readEmpDetails" writer="writeEmpDetails" commit-interval="100">
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
    <batch:step id="deleteProcessedFile">
            <batch:tasklet ref="fileDeletingTasklet" />
    </batch:step>
</batch:job>
<bean id="fileDeletingTasklet" class="com.test.FileDeletingTasklet">
      <property name="directoryResource">
          <bean id="directory" class="org.springframework.core.io.FileSystemResource">
             <constructor-arg value="E:/testDir/file1.txt" />
        </bean>
      </property>
</bean>


推荐答案

覆盖 FlatFileItemReader.setResource ()方法为

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

并在中管理文件删除FlatFileItemReader.read() 当流被完全消耗时

public T read() throws Exception {
  T o = this.delegateReader.read();
  if (o == null) {
    // Perform deletion here
    deleteFile(this.resource);
  }
  return o;
}

这篇关于Spring批处理 - 处理后从目录中删除flatfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:42