gpt4 book ai didi

java - Spring集成(5.0)-文件适配器: Enabling prevent-duplicates in custom scanner is not working

转载 作者:行者123 更新时间:2023-12-02 11:50:31 26 4
gpt4 key购买 nike

文件适配器配置

<int-file:inbound-channel-adapter 
directory="/app/download/client/"
id="clientFileAdapter"
channel="channelOne"
scanner="myFileScanner" prevent-duplicates="true">
<int:poller fixed-delay="1000" max-messages-per-poll="1"/>
</int-file:inbound-channel-adapter>

MyFileScanner.class

@Component("myFileScanner")
public class MyFileScanner implements DirectoryScanner {
@Autotwired
private MyFileFilter myFileFilter;

@Override
public List<File> listFiles(File file)throws IllegalArgumentException{
File[] files = listEligibleFiles(file);
if(files==null){
throw new MessagingException("The path is not valid");
}
if(this.myFileFilter!=null){
return this.myFilter.filterFiles(files);
}
else{
return Arrays.asList(files);
}
}

//Other override methods

protected File[] listEligibleFiles(File directory){
File[] rootFiles = directory.listFiles();
ArrayList files = new ArrayList(rootFiles.length);
for(File rootFile:rootFiles){
if(rootFile.isDirectory()){
files.addAll(Arrays.asList(this.listEligibleFiles(rootFile));
}
else{
files.add(rootFile);
}
}
return (File[])files.toArray(new File[files.size()]);
}
}

MyFileFilter.class

    @Component("myFileFilter")
public class MyFileFilter implements FileListFilter<File>{
@Override
public List<File> filterFiles(File[] files){
ArrayList accepted = new ArrayList();
if(files != null){
for(File f: files){
if(f.getName.endsWith(".DAT"))
accepted.add(f);
}
}
return accepted;
}
}

问题:
如果从文件适配器配置中删除了防止重复标志,则代码可以正常工作,但会一次又一次地选择相同的文件。
如果存在防止重复标志,则会抛出错误 提供的外部“扫描仪”中必须存在“过滤器”和“储物柜”选项:

阅读 spring 5.0 文档后,得到以下信息。
对于外部扫描仪的情况,FileReadingMessageSource 上禁止所有过滤器和锁定器属性;必须在该自定义 DirectoryScanner 上指定它们(如果需要)。换句话说,如果您将扫描器注入(inject) FileReadingMessageSource,则应该在该扫描器上提供过滤器和锁定器,而不是在 FileReadingMessageSource 上。

请提供有关如何启用防止重复标志或自定义实现的建议,以便在使用自定义扫描仪时不再拾取相同的文件。

我的应用程序是否需要缓存文件元数据(名称和文件创建时间戳等),并在适配器拾取文件时在我的 Filter 类中使用它进行比较,以决定是否存在重复文件?

最佳答案

prevent-duplicates="true" 等于 AcceptOnceFileListFilter:

                <xsd:documentation><![CDATA[
A boolean flag indicating whether duplicates should be prevented. If a 'filter' reference is
provided, duplicate prevention will not be enabled by default (the assumption is that the
provided filter is sufficient), but setting this to true will enable it. If a 'filename-pattern'
is provided, duplicate prevention will be enabled by default (preceding the pattern matching),
but setting this to false will disable it. If neither 'filter' or 'filename-pattern' is provided,
duplicate prevention is enabled by default, but setting this to false will disable it. For more
detail on the actual duplicate prevention, see the javadoc for AcceptOnceFileListFilter.
]]></xsd:documentation>

因此,您必须将其与自定义过滤器组合在一起,并将组合提供给扫描仪

关于java - Spring集成(5.0)-文件适配器: Enabling prevent-duplicates in custom scanner is not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com