You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Integration:为入站通道适配器添加空文件与旧文件过滤规则

Spring Integration 入站通道适配器:空文件与旧文件过滤配置

核心实现逻辑

针对你的两个需求,我们可以用Spring Integration提供的CompositeFileListFilter组合两个专用过滤器,分别处理空文件过滤和旧文件过滤,再将这个组合过滤器绑定到入站通道适配器上即可。

具体配置方案

1. Java配置方式

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.FileSizeFileListFilter;
import org.springframework.integration.file.filters.LastModifiedFileListFilter;

import java.io.File;
import java.time.Duration;

@Configuration
public class FileIntegrationConfig {

    // 空文件过滤器:只保留大小≥1字节的文件
    @Bean
    public FileSizeFileListFilter nonEmptyFileFilter() {
        FileSizeFileListFilter filter = new FileSizeFileListFilter();
        filter.setMinSize(1); // 过滤大小为0的空文件
        return filter;
    }

    // 旧文件过滤器:只保留3天以内的文件
    @Bean
    public LastModifiedFileListFilter recentFileFilter() {
        LastModifiedFileListFilter filter = new LastModifiedFileListFilter();
        // 3天对应的毫秒数:3*24*60*60*1000 = 259200000
        filter.setAge(Duration.ofDays(3).toMillis());
        return filter;
    }

    // 组合两个过滤器
    @Bean
    public CompositeFileListFilter<File> compositeFileFilter() {
        CompositeFileListFilter<File> compositeFilter = new CompositeFileListFilter<>();
        compositeFilter.addFilter(nonEmptyFileFilter());
        compositeFilter.addFilter(recentFileFilter());
        // 可选:添加持久化过滤器避免重复处理同一文件
        // compositeFilter.addFilter(new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "fileFilter"));
        return compositeFilter;
    }

    // 配置入站通道适配器与轮询器
    @Bean
    public IntegrationFlow fileInboundFlow() {
        return IntegrationFlows.from(
                        FileReadingMessageSource.createDirectoryScanner(compositeFileFilter()),
                        spec -> spec.poller(p -> p.fixedDelay(5000)) // 轮询间隔5秒
                                .autoStartup(true))
                .channel("fileInputChannel") // 绑定到你的目标通道
                .get();
    }
}

2. XML配置方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-file="http://www.springframework.org/schema/integration/file"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration
        https://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/file
        https://www.springframework.org/schema/integration/file/spring-integration-file.xsd">

    <!-- 空文件过滤器:过滤大小为0的文件 -->
    <bean id="nonEmptyFileFilter" class="org.springframework.integration.file.filters.FileSizeFileListFilter">
        <property name="minSize" value="1"/>
    </bean>

    <!-- 旧文件过滤器:过滤超过3天的文件(259200000毫秒=3天) -->
    <bean id="recentFileFilter" class="org.springframework.integration.file.filters.LastModifiedFileListFilter">
        <property name="age" value="259200000"/>
    </bean>

    <!-- 组合过滤器 -->
    <bean id="compositeFileFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter">
        <property name="filters">
            <list>
                <ref bean="nonEmptyFileFilter"/>
                <ref bean="recentFileFilter"/>
                <!-- 可选:添加持久化过滤器避免重复处理 -->
                <!-- <bean class="org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter">
                    <constructor-arg>
                        <bean class="org.springframework.integration.metadata.SimpleMetadataStore"/>
                    </constructor-arg>
                    <constructor-arg value="fileFilter"/>
                </bean> -->
            </list>
        </property>
    </bean>

    <!-- 入站文件通道适配器 -->
    <int-file:inbound-channel-adapter id="fileInboundAdapter"
                                      directory="file:/path/to/your/directory" <!-- 替换为你的文件目录 -->
                                      channel="fileInputChannel" <!-- 绑定到你的目标通道 -->
                                      filter="compositeFileFilter">
        <int:poller fixed-delay="5000"/> <!-- 轮询间隔5秒 -->
    </int-file:inbound-channel-adapter>

    <!-- 定义目标通道 -->
    <int:channel id="fileInputChannel"/>
</beans>

关键说明

  • FileSizeFileListFilter通过minSize设置最小文件大小,1字节即可过滤掉空文件。
  • LastModifiedFileListFilter的age参数是文件允许的最大过期毫秒数,Java配置下用Duration.ofDays(3).toMillis()更直观。
  • 如果需要避免重复处理同一文件,可以加入FileSystemPersistentAcceptOnceFileListFilter,它会持久化已处理文件的元数据,重启后也不会重复处理。
  • 轮询器的fixed-delay可以根据实际需求调整轮询间隔时间。

内容的提问来源于stack exchange,提问作者Srinu Babu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 22:23:18