Spring 集成 CompositeFileListFilter

Spring Integration CompositeFileListFilter

我正在使用 CompositeFileListFilter 添加两个过滤器。首先,我想使用默认实现执行 AbstractPersistentFileListFilter,从而防止重复。 其次,我使用自己的实现,它会检查数据库中是否存在文件,从而在系统重启时保护我免受重复

这个方法怎么发布?从而先执行带有MetadataStore内部内存的AbstractPersistentFileListFilter的默认实现

目标是减少检查文件是否存在的数据库调用。也许有更好的解决方法。感谢您的帮助!

FtpConfiguration.java

@Bean
    CompositeFileListFilter<FTPFile> getCompositeFileListFilter() {
        CompositeFileListFilter<FTPFile> compositeFileListFilter = new CompositeFileListFilter<>();
        compositeFileListFilter.addFilters(new CustomFileFilter(messageRepo));
        return compositeFileListFilter;
    }

    @Bean
    public IntegrationFlow ftpIntegrationFlow() {
        return IntegrationFlows.from(
                Ftp.inboundStreamingAdapter(template())
                        .filter(getCompositeFileListFilter())
                        .remoteDirectory("."),
                e -> e.poller(Pollers.fixedDelay(500).advice(advice())))
                .transform(new StreamTransformer("UTF-8"))
                .handle(messageService::unmarshall)
                .get();
    }

CustomFileFilter.java

@Component
@Log4j2
public class CustomFileFilter implements FileListFilter<FTPFile> {

    private final MessageRepo messageRepo;

    public CustomFileFilter(MessageRepo messageRepo) {
        this.messageRepo = messageRepo;
    }

    @Override
    public List<FTPFile> filterFiles(FTPFile[] files) {
        return null;
    }

    @Override
    public boolean accept(FTPFile file) {
        String fileName = file.getName();
        log.info("file filter get name: {}", fileName);

        Integer fileCheck = messageRepo.checkExistsMessage(fileName);
        log.info("fileCheck: {}", fileCheck);

        return fileCheck != 1;
    }

    @Override
    public boolean supportsSingleFileFiltering() {
        return true;
    }
}

改用ChainFileListFilter

/**
 * The {@link CompositeFileListFilter} extension which chains the result
 * of the previous filter to the next one. If a filter in the chain returns
 * an empty list, the remaining filters are not invoked.
 *
 * @param <F> The type that will be filtered.
 *
 * @author Artem Bilan
 * @author Gary Russell
 * @author Cengis Kocaurlu
 *
 * @since 4.3.7
 *
 */
public class ChainFileListFilter<F> extends CompositeFileListFilter<F> {

https://docs.spring.io/spring-integration/docs/current/reference/html/file.html#file-reading

Starting with version 4.3.7, a ChainFileListFilter (an extension of CompositeFileListFilter) has been introduced to allow scenarios when subsequent filters should only see the result of the previous filter. (With the CompositeFileListFilter, all filters see all the files, but it passes only files that have passed all filters). ...