Spring Integration(5.0) - 文件适配器:启用自定义扫描程序中的防止重复项不起作用

时间:2017-12-20 09:37:11

标签: java spring spring-integration

文件适配器配置

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

问题:
如果从文件适配器配置中删除了prevent-duplicates标志,则代码可以正常工作,但会一次又一次地拾取相同的文件。
如果出现prevent-duplicates标志,则会抛出错误过滤器&#39;和&#39;储物柜&#39;选项必须出现在提供的外部扫描仪&#39;:

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

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

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

1 个答案:

答案 0 :(得分:1)

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提供合成。