文件适配器配置
<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;
}
}
问题:
如果从文件适配器配置中删除了预防性标志,则代码正常工作,但一次又一次选择相同的文件。
如果存在预防性标志,则必须出于"滤镜"one_answers"储物柜"选项丢失错误的外部"扫描仪":
阅读了春季5.0文档后,获得了以下信息。
对于外部扫描仪的情况,所有过滤器和储物柜属性被禁止在FileReadingMessagesOurce上;必须在该自定义DirectoryScanner上(如果需要)指定它们。换句话说,如果您将扫描仪注入fileReadingMessageSource,则应在该扫描仪上提供过滤器和储物柜,而不是在fileReadingMessageSource上。
请提供有关如何在使用自定义扫描仪时启用预防性标志或自定义实现的建议。
我的应用程序需要缓存文件元数据(名称和文件创建时间戳等),并在Adapter挑选文件以确定重复文件时使用它在我的过滤类中进行比较吗?
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>
因此,您必须将其与自定义过滤器一起组成,并为scanner
提供组成。