1
votes

I'm trying to make a File Outbound Channel Adapter to write a file having the last modified date attribute set to a custom value instead of system current time.

according to the documentation (http://docs.spring.io/spring-integration/docs/4.3.11.RELEASE/reference/html/files.html#file-timestamps) I'm supposed to set the preserve-timestamp attribute to true on the outbound and set the header file_setModified to the desired timestamp in the messages.

Anyway I made several attempts without success.

This is a code snippet to show what I'm doing right now:

<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"/>

what's wrong with that?

(using Spring Integration 4.3.11)

1

1 Answers

1
votes

The timestamp value is overridden if your payload is a File:

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());
    }
}

To avoid that override and really get a gain from the file_setModified, you should convert the File from the <int:inbound-channel-adapter> to its InputStream:

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

before <int-file:outbound-channel-adapter>.

The documentation warns about that though:

For File payloads, this will transfer the timestamp from the inbound file to the outbound (regardless of whether a copy was required)

UPDATE

I have just tested your use case and my /tmp/out directory looks like:

enter image description here

As you see all my files have the proper custom last modified.

What am I missing?

Maybe that 1473897600 (1970 year) is wrong for your operation system?

UPDATE

OK! The problem that preserve-timestamp isn't configured into the target component during parsing that XML: https://jira.spring.io/browse/INT-4324

The workaround for your use-case is like:

<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>

instead of that <int-file:outbound-channel-adapter> definition.