Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring集成(5.0)-文件适配器:在自定义扫描仪中启用“防止重复”功能不起作用_Java_Spring_Spring Integration - Fatal编程技术网

Java Spring集成(5.0)-文件适配器:在自定义扫描仪中启用“防止重复”功能不起作用

Java Spring集成(5.0)-文件适配器:在自定义扫描仪中启用“防止重复”功能不起作用,java,spring,spring-integration,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="

文件适配器配置

<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()]);
    }
}
@组件(“myFileScanner”)
公共类MyFileScanner实现DirectoryScanner{
@自动连线
私有MyFileFilter MyFileFilter;
@凌驾
公共列表列表文件(文件文件)引发IllegalArgumentException{
File[]files=listEligibleFiles(文件);
if(files==null){
抛出新的MessaginException(“路径无效”);
}
if(this.myFileFilter!=null){
返回此.myFilter.filterFiles(文件);
}
否则{
返回Arrays.asList(文件);
}
}
//其他覆盖方法
受保护的文件[]ListLigibleFile(文件目录){
File[]rootFiles=directory.listFiles();
ArrayList files=新的ArrayList(rootFiles.length);
用于(文件根文件:根文件){
if(rootFile.isDirectory()){
files.addAll(Arrays.asList(this.listEligibleFiles(rootFile));
}
否则{
添加(rootFile);
}
}
返回(文件[])files.toArray(新文件[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;
        }
    }
@组件(“myFileFilter”)
公共类MyFileFilter实现FileListFilter{
@凌驾
公共列表筛选器文件(文件[]文件){
ArrayList accepted=新的ArrayList();
如果(文件!=null){
用于(文件f:文件){
if(f.getName.endsWith(“.DAT”))
接受。添加(f);
}
}
接受退货;
}
}
问题:
如果从文件适配器配置中删除了“防止重复”标志,则代码工作正常,但会一次又一次地拾取相同的文件。
如果存在“防止重复”标志,则会引发错误。提供的外部“扫描仪”上必须存在“过滤器”和“锁定器”选项:

在阅读了spring 5.0文档后,获得了以下信息。
对于外部扫描仪,FileReadingMessageSource上禁止所有筛选器和locker属性;必须指定它们(如果需要)换句话说,如果将扫描仪插入FileReadingMessageSource,则应在该扫描仪上而不是FileReadingMessageSource上提供筛选器和锁定器

请提供建议,说明如何启用“防止重复”标志或自定义实现,以便在使用自定义扫描仪时不再拾取同一文件


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

防止重复=“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>

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