【问题标题】:Spring Integration File Outbound Channel Adapter and file last modified dateSpring Integration File Outbound Channel Adapter 和文件最后修改日期
【发布时间】:2017-08-03 08:29:15
【问题描述】:

我正在尝试制作一个文件出站通道适配器来写入一个将上次修改日期属性设置为自定义值而不是系统当前时间的文件。

根据文档 (http://docs.spring.io/spring-integration/docs/4.3.11.RELEASE/reference/html/files.html#file-timestamps),我应该在出站上将preserve-timestamp 属性设置为true,并将标头file_setModified 设置为消息中所需的时间戳。

反正我试了几次都没有成功。

这是一个代码 sn-p 来显示我现在在做什么:

<int:inbound-channel-adapter
    channel="msg.channel"
    expression="'Hello'">
    <int:poller fixed-delay="1000"/>
</int:inbound-channel-adapter>

<int:header-enricher
    input-channel="msg.channel"
    output-channel="msgEnriched.channel">

    <int:header
        name="file_setModified"
        expression="new Long(1473897600)"/>
</int:header-enricher>

<int-file:outbound-channel-adapter
    id="msgEnriched.channel"
    preserve-timestamp="true"
    directory="/tmp/foo"/>

这有什么问题?

(使用 Spring Integration 4.3.11)

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    如果您的payloadFile,则timestamp 值将被覆盖:

    Object timestamp = requestMessage.getHeaders().get(FileHeaders.SET_MODIFIED);
    ...
    if (payload instanceof File) {
        resultFile = handleFileMessage((File) payload, tempFile, resultFile);
        timestamp = ((File) payload).lastModified();
    }
    ...
    if (this.preserveTimestamp) {
        if (timestamp instanceof Number) {
            resultFile.setLastModified(((Number) timestamp).longValue());
        }
    }
    

    为避免这种覆盖并真正从 file_setModified 中获得收益,您应该将 File&lt;int:inbound-channel-adapter&gt; 转换为其 InputStream

    <transformer expression="new java.io.FileInputStream(payload)"/>
    

    &lt;int-file:outbound-channel-adapter&gt;之前。

    文档对此提出警告:

    对于文件负载,这会将时间戳从入站文件传输到出站文件(无论是否需要副本)

    更新

    我刚刚测试了你的用例,我的/tmp/out 目录看起来像:

    如您所见,我的所有文件都有正确的自定义上次修改。

    我错过了什么?

    也许1473897600(1970 年)对您的操作系统有误?

    更新

    好的!解析XML时preserve-timestamp未配置到目标组件的问题:https://jira.spring.io/browse/INT-4324

    您的用例的解决方法如下:

    <int:outbound-channel-adapter id="msgEnriched.channel">
        <bean class="org.springframework.integration.file.FileWritingMessageHandler">
            <constructor-arg value="/tmp/foo"/>
            <property name="preserveTimestamp" value="true"/>
            <property name="expectReply" value="false"/>
        </bean>
    </int:outbound-channel-adapter>
    

    而不是 &lt;int-file:outbound-channel-adapter&gt; 定义。

    【讨论】:

    • 感谢 Artem 的澄清。但是,当有效负载不是文件的情况下,就像我发布的示例中那样(并且它发生在我的真实用例中)?无论如何,它似乎不起作用。
    • 您发布的示例正是关于File 有效负载&lt;int:inbound-channel-adapter&gt; 生成文件。您可以调试FileWritingMessageHandler.handleRequestMessage() 并检查您的有效负载发生了什么。
    • 有些东西我不明白。入站通道适配器中的expression="'Hello'" 不是产生Strings 吗?
    • Doh...我现在看到了您的代码。让我在本地测试一下!无论如何:如何调试提及代码?
    • 在我的回答中再查看一个更新。
    猜你喜欢
    • 2017-04-28
    • 2013-09-28
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多