我正在使用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;
}
}
最佳答案
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
必须在列表中排在首位...