在Spring Integration Listener中,有一个类文件调用FileReadingMessageSource
,有一个布尔变量允许编码器通过其方法setScanEachPoll(boolean scanEachPoll)
对其进行修改。
我设法做到了,监听器将在每个周期扫描目录。但是,我发现了一些错误,即toBeReceived
队列每次扫描目录时都会将文件追加到文件中。
例如,文件a,b,c,d,e。
第1个周期,扫描,队列将有a,b,c,d,e - >处理a,队列左b,c,d,e
第二个周期,扫描,队列将有b,c,d,e,b,c,d,e - >进程b,队列左c,d,e,b,c,d,e 它在队列中将有重复的文件名。
此处粘贴scanInputDirectory()
类中的FileReadingMessageSource
方法。
private void scanInputDirectory() {
List<File> filteredFiles = scanner.listFiles(directory);
Set<File> freshFiles = new LinkedHashSet<File>(filteredFiles);
if (!freshFiles.isEmpty()) {
toBeReceived.addAll(freshFiles);
if (logger.isDebugEnabled()) {
logger.debug("Added to queue: " + freshFiles);
}
}
}
任何想法如何防止这种情况?
答案 0 :(得分:1)
我仍然认为AcceptOnceFileListFilter
不允许从目标目录中提取相同的文件:请参阅DefaultDirectoryScanner.listFiles()
以及它如何使用this.filter
。
OTOH可以通过internalQueueCapacity
的{{1}} ctor arg修复OOM问题:
FileReadingMessageSource
然后查看该类的JavaDocs:
/**
* Creates a FileReadingMessageSource with a bounded queue of the given
* capacity. This can be used to reduce the memory footprint of this
* component when reading from a large directory.
*
* @param internalQueueCapacity
* the size of the queue used to cache files to be received
* internally. This queue can be made larger to optimize the
* directory scanning. With scanEachPoll set to false and the
* queue to a large size, it will be filled once and then
* completely emptied before a new directory listing is done.
* This is particularly useful to reduce scans of large numbers
* of files in a directory.
*/
public FileReadingMessageSource(int internalQueueCapacity) {
this(null);
Assert.isTrue(internalQueueCapacity > 0,
"Cannot create a queue with non positive capacity");
this.scanner = new HeadDirectoryScanner(internalQueueCapacity);
}
但是它不能与 * A custom scanner that only returns the first <code>maxNumberOfFiles</code>
* elements from a directory listing. This is useful to limit the number of File
* objects in memory and therefore mutually exclusive with {@code AcceptOnceFileListFilter}.
* It should not be used in conjunction with an {@code AcceptOnceFileListFilter}.
一起使用,最后必须删除已处理的文件,以避免在下次轮询时使用。
另请参阅Reference Manual。
中的更多信息