如何在将文件名发送到Spring Batch之前更改文件名?

时间:2014-07-22 17:10:49

标签: spring spring-batch spring-integration

我正在使用Spring Integration对目录进行文件轮询。但是我希望在完成任何操作之前在文件名上附加时间戳并发送到spring批处理。

这可能吗?

2 个答案:

答案 0 :(得分:3)

下面的示例是在Spring Integration samples on github之一的帮助下创建的。

这是相同的示例,经过修改以演示添加到输出文件名的时间戳:

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

上面代码中最相关的部分是:filename-generator-expression="new java.text.SimpleDateFormat('yyyyMMddHHmmssSSS').format(new java.util.Date()) + '.' + headers['file_name']",它表明使用简单的SpEL表达式,输出文件具有所需的名称。

答案 1 :(得分:2)

是的,你可以。基本上,您需要配置输入通道,处理器/处理程序和输出通道。输入表示输入文件。处理器/处理程序重命名文件,输出将其移动到弹出批处理的输出目录。

使用spring-integration-examples作为我的参考(无论如何都值得一试):

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