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

Spring Integration SftpInboundAdapter按任务拉取文件及相关问题求助

问题1:ChannelInterceptor重复添加
  • 最优方案:避免在每次getFile方法执行时动态添加拦截器,在Spring配置阶段全局注册一次即可:
    1. 把自定义拦截器声明为单例Bean
    2. 在定义sftpInputChannel时直接注入拦截器,示例代码如下:
    @Bean
    public ChannelInterceptor sftpFileCaptureInterceptor() {
        return new ChannelInterceptor() {
            @Override
            public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
                // 实现你的文件内容捕获逻辑
            }
        };
    }
    
    @Bean
    public MessageChannel sftpInputChannel(ChannelInterceptor sftpFileCaptureInterceptor) {
        PublishSubscribeChannel channel = new PublishSubscribeChannel();
        channel.addInterceptor(sftpFileCaptureInterceptor);
        return channel;
    }
    
  • 若必须动态添加,添加前先做存在性校验:
    boolean interceptorExisted = sftpInputChannel.getInterceptors()
            .stream()
            .anyMatch(interceptor -> interceptor.getClass() == SftpFileCaptureInterceptor.class);
    if (!interceptorExisted) {
        sftpInputChannel.addInterceptor(sftpFileCaptureInterceptor);
    }
    
问题2:后续任务无法触发SFTP拉取

你只清空了本地文件过滤器的元数据,SFTP入站适配器默认还存在远程文件拉取记录缓存,同名文件拉取过一次后默认不会重复拉取,可按以下方案修改:

  • 方案1:替换远程文件过滤器为AcceptAllFileListFilter,适合你固定文件名、每次任务都要拉取的场景,配置示例:
@Bean
public IntegrationFlow sftpInboundFlow(SftpSessionFactory sftpSessionFactory) {
    return IntegrationFlows.from(Sftp.inboundAdapter(sftpSessionFactory)
                    .autoStartup(false)
                    .remoteDirectory("你的远程文件目录")
                    // 关闭远程文件过滤,每次执行都拉取目标文件
                    .filter(new AcceptAllFileListFilter<ChannelSftp.LsEntry>())
                    .localFilter(new FileSystemPersistentAcceptOnceFileListFilter(simpleMetadataStore, "sftp-local-"))
                    .localDirectory(new File("你的本地存储路径")))
            .channel(sftpInputChannel)
            .get();
}
  • 方案2:如果需要保留远程过滤能力,每次任务执行完成后同时清空远程过滤器关联的元数据存储,并且给远程过滤器设置flushOnUpdate = true。
  • 额外校验:确认CachingSessionFactory的会话缓存没有异常,可配置setCacheSession(false)每次拉取新建会话,避免会话复用导致的无连接日志问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 17:06:02