samwell
samwell

Reputation: 2797

How do I change the file name before sending it to Spring Batch?

I'm using Spring Integration to do a file polling on a directory. But I want to append the time stamp on the file name before anything is done to it and sent to spring batch.

Is this possible?

Upvotes: 0

Views: 4569

Answers (2)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Sample below created with the help of one of the Spring Integration samples on github.

Here's the same sample, modified to demonstrate the timestamp being added to the output file name:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

<file:inbound-channel-adapter id="filesIn"
                              directory="file:${java.io.tmpdir}/spring-integration-samples/input"
                              filename-regex="[a-z]+.txt">
    <integration:poller id="poller" fixed-delay="5000"/>
</file:inbound-channel-adapter>

<file:file-to-string-transformer input-channel="filesIn" output-channel="strings"/>

<integration:channel id="strings"/>

<integration:service-activator input-channel="strings"
                               output-channel="filesOut"
                               ref="handler"/>

<file:outbound-channel-adapter id="filesOut" directory="file:${java.io.tmpdir}/spring-integration-samples/output"
     filename-generator-expression="new java.text.SimpleDateFormat('yyyyMMddHHmmssSSS').format(new java.util.Date()) + '.' + headers['file_name']" />

<bean id="handler" class="org.springframework.integration.samples.filecopy.Handler"/>

The most relevant part from the code above is this: filename-generator-expression="new java.text.SimpleDateFormat('yyyyMMddHHmmssSSS').format(new java.util.Date()) + '.' + headers['file_name']" which shows that with a simple SpEL expression your output file has the desired name.

Upvotes: 3

Shiraaz.M
Shiraaz.M

Reputation: 3191

Yes you can. Basically you want to configure an input channel, a processor/handler and an output channel. The input represents the input files. The processor/handler renames the files and the output moves this to the output directory for spring batch.

Using the spring-integration-examples as my reference (and worth checking out anyway):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:integration="http://www.springframework.org/schema/integration"
    xmlns:file="http://www.springframework.org/schema/integration/file"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/integration
            http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/file
            http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

    <file:inbound-channel-adapter id="filesIn"
                                  directory="file:${java.io.tmpdir}/spring-integration-samples/input">
        <integration:poller id="poller" fixed-delay="5000"/>
    </file:inbound-channel-adapter>

    <integration:service-activator input-channel="filesIn"
                                   output-channel="filesOut"
                                   ref="handler"/>

    <file:outbound-channel-adapter id="filesOut"
                                   directory="file:${java.io.tmpdir}/spring-integration-samples/output"
                                   delete-source-files="true"/>

    <bean id="handler" class="org.springframework.integration.samples.filecopy.Handler"/>

</beans>

Upvotes: 2

Related Questions