【问题标题】:Spring Integration - Read file by nameSpring Integration - 按名称读取文件
【发布时间】:2015-03-26 11:34:23
【问题描述】:

我想使用 Spring Integration 按名称读取文件。我知道如何将文件适配器与轮询器一起使用。但在这种情况下,我只想按名称读取文件并将其添加到输入通道。

我的应用程序是一个独立的 jar,将使用命令行参数调用。参数之一是文件名。我也在使用 Spring-Boot。只是不知道如何通过读取此文件并将其添加到输入通道来启动处理。我需要自定义网关吗?

谢谢

【问题讨论】:

    标签: java spring integration spring-boot spring-integration


    【解决方案1】:

    如果我理解正确,您需要使用<context:property-placeholder> 并进行如下配置:

    <file:inbound-channel-adapter
            channel="input"
            directory="${dir}"
            filename-pattern="${pattern}">
        <poller fixed-delay="${fixedDelay}" time-unit="SECONDS"/>
    </file:inbound-channel-adapter>
    

    所有这些占位符都应该是您的程序参数,如下所示:

    java myMain --dir=/usr/mydir --patter=myfile.name --fixedDelay=1000
    

    更新

    但是,不:您不需要&lt;context:property-placeholder&gt; - Spring Boot 为您做到了!

    更新 2

    对不起,您的要求还不清楚。无论如何,这个怎么样:

    <int-event:inbound-channel-adapter channel="processFileChannel"
        event-types="org.springframework.context.event.ContextRefreshedEvent"
        payload-expression="new java.io.File('${filePath}')"/>
    

    filePath 又是一个命令行参数。以及来自 Spring Boot 的属性占位符。

    &lt;int-event:inbound-channel-adapter&gt;负责收听ApplicationEvent。在这种情况下,我们会对 ContextRefreshedEvent 做出反应 - 当您的 applicationContext 准备好做某事时的主要事件。

    【讨论】:

    • 其实没有。我只想按名称调用特定文件。每次都会有所不同。没有模式。就像我在问题中提到的那样,我不需要轮询器,因为我不会有任何轮询,因为这是一个独立的 jar,而不是在 Web 服务器上运行。
    • 用另一个解决方案更新了我的答案。
    • 这很有趣..会尝试让您知道,谢谢
    猜你喜欢
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多