Spring批处理

编程入门 行业动态 更新时间:2024-10-26 11:21:10
本文介绍了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批处理

本文发布于:2023-08-01 20:16:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批处理   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!