FileSystemPersistentAcceptOnceFileListFilter与CompositeFileListFilter不兼容

时间:2015-08-19 14:12:28

标签: spring spring-integration

我正在使用int-sftp:inbound-channel-adapter观看多个远程文件夹,但我遇到了本地过滤器问题。

<int-sftp:inbound-channel-adapter id="inboundChannelPmse"
            session-factory="sftpSessionFactory"
            channel="chan"
            remote-directory="${rdir1}"
            filter="remoteUnseenFilter"
            preserve-timestamp="true"
            local-directory="${ldir1}"
            auto-create-local-directory="true"
            temporary-file-suffix=".writing"
            local-filter="localOnlyXmlFilter"
            delete-remote-files="false"
            local-filename-generator-expression="#this.toLowerCase()"
            >
        <int:poller fixed-rate="10000" max-messages-per-poll="-1" />
</int-sftp:inbound-channel-adapter>

<int-sftp:inbound-channel-adapter id="inboundChannelOfcomDefault"
            session-factory="sftpSessionFactory"
            channel="chan"
            remote-directory="${rdir2}"
            filter="remoteUnseenFilter"
            preserve-timestamp="true"
            local-directory="${ldir2}"
            auto-create-local-directory="true"
            temporary-file-suffix=".writing"
            local-filter="localOnlyCsvFilter"
            delete-remote-files="false"
            local-filename-generator-expression="#this.toLowerCase()"
            >
        <int:poller fixed-rate="10000" max-messages-per-poll="-1" />
</int-sftp:inbound-channel-adapter>

每个过滤器都是一个复合过滤器,它包含一个FileSystemPersistentAcceptOnceFileListFilter和另一个过滤器,例如IgnoreHiddenFileListFilter和/或我的ExtensionFileListFilter,例如。

<bean id="localOnlyXmlFilter"
    class="org.springframework.integration.file.filters.CompositeFileListFilter">
    <constructor-arg>
        <list>
            <bean
                class="org.springframework.integration.file.filters.IgnoreHiddenFileListFilter" />
            <bean
                class="uk.co.bigsoft.app.imports.filters.ExtensionFileListFilter">
                <constructor-arg value="xml" />
            </bean>
            <bean
                class="uk.co.bigsoft.app.imports.filters.PrefixFileListFilter">
                <constructor-arg index="0" value="pref2_" />
            </bean>
            <bean class="org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter">
                <constructor-arg index="0" ref="localFileStore" />
                <constructor-arg index="1" name="prefix" value="" />
                <property name="flushOnUpdate" value="true" />
            </bean>
        </list>
    </constructor-arg>
</bean>

<bean id="localOnlyCsvFilter"
    class="org.springframework.integration.file.filters.CompositeFileListFilter">
    <constructor-arg>
        <list>
            <bean
                class="org.springframework.integration.file.filters.IgnoreHiddenFileListFilter" />
            <bean
                class="uk.co.bigsoft.app.imports.filters.ExtensionFileListFilter">
                <constructor-arg value="csv" />
            </bean>
            <bean
                class="uk.co.bigsoft.app.imports.filters.PrefixFileListFilter">
                <constructor-arg index="0" value="pref1_" />
            </bean>
            <bean class="org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter">
                <constructor-arg index="0" ref="localFileStore" />
                <constructor-arg index="1" name="prefix" value="" />
                <property name="flushOnUpdate" value="true" />
            </bean>
        </list>
    </constructor-arg>
</bean>

该文件下载正常,但取决于localOnlyXmlFilter或localOnlyCsvFilter是否先运行,第二次运行的文件不会看到它,因为它已经被首先运行的那个记住了!我想我真正想要的是本地过滤器沿着过滤器列表向下移动,当过滤器为假(或不返回任何条目)时停止而不是运行所有过滤器(这似乎是对我毫无意义。)

有什么能做到的吗?我一直在考虑重组过滤器,但我一直遇到问题。

public class StoppingCompositeFileFilter extends AbstractFileListFilter<File> {

    private List<AbstractFileListFilter<File>> filters;

    public StoppingCompositeFileFilter(List<AbstractFileListFilter<File>> filters) {
    this.filters = filters;
    }

    @Override
    protected boolean accept(File file) {
    for (AbstractFileListFilter<File> filter : filters) {
       -->      // Fails to compile because accept() is abstract for AbstractFileListFilter
        if (!filter.accept(file)) {
            return false;
        }
    }
    return true;

    }
}

或者看起来过于复杂的那个:

class AnotherFilter implements FileListFilter<File> {

    private List<FileListFilter<File>> filters;


    @Override
    public List<File> filterFiles(File[] files) {
        List<File> keeping = new ArrayList<File>();
        for (File file : files) {
            for (FileListFilter<File> filter : filters) {
                keeping = filter.filterFiles(keeping.toArray(new File[keeping.size()]));
                if (keeping.size() == 0) {
                    return new ArrayList<File>();
                }
            }
        }
        return keeping;
    }

}

1 个答案:

答案 0 :(得分:1)

CompositeFileListFilter逻辑如下:

public List<F> filterFiles(F[] files) {
    Assert.notNull(files, "'files' should not be null");
    List<F> results = new ArrayList<F>(Arrays.asList(files));
    for (FileListFilter<F> fileFilter : this.fileFilters) {
        List<F> currentResults = fileFilter.filterFiles(files);
        results.retainAll(currentResults);
    }
    return results;
}

所以,我认为你应该根据现有算法构建自己的逻辑,而不是发明一个可能错误或足够复杂的新设计。

尚不确定您想要达到的目标,但在我看来,AcceptOnce必须是列表中的第一个...