如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

SFTP 流式入站通道适配器

版本 4.3 引入了流式传入通道适配器。 该适配器生成负载类型为 InputStream 的消息,使您能够在不写入本地文件系统的情况下获取文件。 由于会话保持打开状态,消费应用程序负责在文件被消费后关闭会话。 会话通过 closeableResource 头(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE)提供。 标准框架组件,如 FileSplitterStreamTransformer,会自动关闭会话。 有关这些组件的更多信息,请参阅 文件拆分器流转换器。 以下示例展示了如何配置 SFTP 流式传入通道适配器:spring-doc.cadn.net.cn

<int-sftp:inbound-streaming-channel-adapter id="ftpInbound"
            channel="ftpChannel"
            session-factory="sessionFactory"
            filename-pattern="*.txt"
            filename-regex=".*\.txt"
            filter="filter"
            filter-expression="@myFilterBean.check(#root)"
            remote-file-separator="/"
            comparator="comparator"
            max-fetch-size="1"
            remote-directory-expression="'foo/bar'">
        <int:poller fixed-rate="1000" />
</int-sftp:inbound-streaming-channel-adapter>

您只能使用 filename-patternfilename-regexfilterfilter-expression 中的一个。spring-doc.cadn.net.cn

从 5.0 版本开始,默认情况下,SftpStreamingMessageSource 适配器会基于内存中的 SimpleMetadataStore 使用 SftpPersistentAcceptOnceFileListFilter 来防止远程文件重复。 默认情况下,此过滤器也会与文件名模式(或正则表达式)一起应用。 如果您需要允许重复,可以使用 AcceptAllFileListFilter。 您可以通过使用 CompositeFileListFilter(或 ChainFileListFilter)来处理任何其他用例。 稍后显示的 Java 配置 所示 展示了一种在处理后删除远程文件以避免重复的技术。

有关 SftpPersistentAcceptOnceFileListFilter 的更多信息及其用法,请参阅 远程持久文件列表过滤器spring-doc.cadn.net.cn

您可以使用 max-fetch-size 属性来限制在每次轮询(poll)时获取的文件数量,仅在必要时进行获取。 将其设置为 1,并在集群环境中运行时使用持久化过滤器。 有关更多信息,请参阅 入站通道适配器:控制远程文件获取spring-doc.cadn.net.cn

适配器将远程目录和文件名放入请求头(分别为FileHeaders.REMOTE_DIRECTORYFileHeaders.REMOTE_FILE)。 从版本5.0开始,FileHeaders.REMOTE_FILE_INFO请求头提供额外的远程文件信息(以JSON格式)。 如果您在SftpStreamingMessageSource上将fileInfoJson属性设置为false,则该请求头将包含一个SftpFileInfo对象。 您可以通过调用SftpFileInfo.getFileInfo()方法访问底层SftpClient提供的SftpClient.DirEntry对象。 当使用XML配置时,fileInfoJson属性不可用,但您可以将SftpStreamingMessageSource注入到其中一个配置类中来设置它。 另请参阅远程文件信息spring-doc.cadn.net.cn

使用 Java 配置进行配置

以下 Spring Boot 应用程序展示了如何使用 Java 配置入站适配器的示例:spring-doc.cadn.net.cn

@SpringBootApplication
public class SftpJavaApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SftpJavaApplication.class)
            .web(false)
            .run(args);
    }

    @Bean
    @InboundChannelAdapter(channel = "stream")
    public MessageSource<InputStream> ftpMessageSource() {
        SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template());
        messageSource.setRemoteDirectory("sftpSource/");
        messageSource.setFilter(new AcceptAllFileListFilter<>());
        messageSource.setMaxFetchSize(1);
        return messageSource;
    }

    @Bean
    @Transformer(inputChannel = "stream", outputChannel = "data")
    public org.springframework.integration.transformer.Transformer transformer() {
        return new StreamTransformer("UTF-8");
    }

    @Bean
    public SftpRemoteFileTemplate template() {
        return new SftpRemoteFileTemplate(sftpSessionFactory());
    }

    @ServiceActivator(inputChannel = "data", adviceChain = "after")
    @Bean
    public MessageHandler handle() {
        return System.out::println;
    }

    @Bean
    public ExpressionEvaluatingRequestHandlerAdvice after() {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setOnSuccessExpression(
                "@template.remove(headers['file_remoteDirectory'] + '/' +  headers['file_remoteFile'])");
        advice.setPropagateEvaluationFailures(true);
        return advice;
    }

}

请注意,在此示例中,转换器下游的消息处理器具有一个 advice,用于在处理完成后删除远程文件。spring-doc.cadn.net.cn