Spring batch-Remove the flatfile from the directory after processing

In spring package, I use MultiResourceItemReader to read multiple files from a directory. Then I use FlatFileItemReader as a delegate to process individual files. My usecase should delete the file after it has been completely processed (READ-WRITE completed), and then multiResourceitemReader should select another file, and it should continue.

I tried FileDeletingTasklet to delete a file in a directory, but according to spring docs, the execute method will only be called once. How can I achieve deletion in a processed file (READ-WRITE), but I do not want the entire directory to be deleted as soon as all files are completely processed in the directory.

The following is the work I use:

<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>
+4
2

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;
}
+3

, jobcontext , .

+1

Source: https://habr.com/ru/post/1538266/


All Articles