java - 将 channel 链接在一起会删除 file_originalFile header

标签 java spring-integration

我有一个要求,我想使用 ftp 并将文件放入动态本地目录中。我还想添加年龄过滤器。那是不允许旧文件通过。这适用于文件正确。我添加了一个自定义过滤器并使用以下代码检查了文件对象创建日期:

int ageLimit = Integer.parseInt(props.getProperty("file.age"));
        BasicFileAttributes view = null;
        try
        {
            view = Files.getFileAttributeView(
                    Paths.get(f.getAbsolutePath()),
                    BasicFileAttributeView.class).readAttributes();
        } catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        FileTime ft = view.creationTime();
        if (((new Date()).getTime() - (((ft.to(TimeUnit.MILLISECONDS))/* / 10000L) - 11644473600000L*/))) > ageLimit
                * (24 * 60 * 60 * 1000))// file creation
                                        // date(converted
                                        // to
                                        // java.util.date)
                                        // - current
                                        // date >
                                        // oldness in ms
        {
            logger.info("File is old:" + (f.getName()));
            return false;
        }

这对于 int 文件适配器来说效果很好。但是当我添加 ftp 支持时,我在有效负载中获得的对象是 FTPFile 对象。这不会给出创建日期,但会给出最后修改日期。最后修改日期对我来说没有用。因此我必须配置 ftp,然后将文件适配器链接到该目录。

  1. int-ftp:inbound-channel-adapter 从 ftp 站点获取文件并将其放入本地目录中。

  2. 该文件由 int-file:inbound-channel-adapter 拾取并放入最终目的地。

这是获取放置在 ftp 位置的文件的两步过程。

过滤器类适用于 int-file:inbound-channel-adapter。这种链接正在发挥作用。

问题在于删除了原始文件。 ftp 站点文件(远程目录)会自动删除。 int-file:inbound-channel-adapter 拾取的本地目录文件不会被删除。

这是我的配置。

     <int:channel id="ftpChannel"/> 
 <int-ftp:outbound-channel-adapter id="ftpOutbound"
    channel="ftpChannel"
    session-factory="ftpClientFactory"
    charset="UTF-8"
    remote-file-separator="/"
    auto-create-directory="true"
    remote-directory="." 
    use-temporary-file-name="true"
    />  
<int-ftp:inbound-channel-adapter id="ftpInbound"
    channel="ftpChannel"
    session-factory="ftpClientFactory"
    charset="UTF-8"

    local-directory="file:${paths.root}"
    delete-remote-files="true"
    temporary-file-suffix=".writing"
    remote-directory="."
     filename-pattern="*${file.char}*${file.char}*${file.char}*${file.char}*${file.char}*"

    preserve-timestamp="true"
     auto-startup="true">
    <int:poller fixed-rate="1000"/>
    </int-ftp:inbound-channel-adapter>
        <bean id="ftpClientFactory"
    class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    <property name="host" value="${ftp.ip}"/>
    <property name="port" value="${ftp.port}"/>
    <property name="username" value="${ftp.username}"/>
    <property name="password" value="${ftp.password}"/>
    <property name="clientMode" value="0"/>
    <property name="fileType" value="2"/>
    <property name="bufferSize" value="100000"/>
</bean>

        <int-file:outbound-channel-adapter channel="abc" id="filesOut"
        directory-expression="@outPathBean.getPath()"
        delete-source-files="true" filename-generator="fileNameGenerator" />

 <int:header-enricher input-channel="ftpChannel" output-channel="ftpChannel">
    <int:header name="file_originalFile"  ref="getPath" method="getCurrentPath" />
</int:header-enricher>

        <int-file:inbound-channel-adapter id="filesIn" directory="file:${paths.root}" channel="abc" filter="compositeFilter" >
        <int:poller id="poller" fixed-delay="5000" />

    </int-file:inbound-channel-adapter>
    <int:channel id="abc"/>

还定义了 customfilefilter 和 filenamegenerator bean。

我添加了 header 丰富器来添加 file_originalFile header ,因为我在某处读到,如果文件使用两次/像我的情况一样链接,则该 header 将被删除。尽管调用了 beans 方法并且我在其中编写了以下代码,但该文件并未从文件适配器源目录中删除。

public String getCurrentPath(Message<File> payload)
{


    File f = payload.getPayload();
    if (payload.getHeaders().get(FileHeaders.ORIGINAL_FILE)== null)
    {
        return f.getAbsolutePath();
    }
    else
    {
        payload.getHeaders().get(FileHeaders.ORIGINAL_FILE).toString();
    }
    return null;
}

我做错了什么?我也尝试过对 abc channel 使用 header-enricher,但运气不佳!

希望问题现在已经解决了..感谢您的帮助

最佳答案

关于:

Cannot convert value of type [org.springframework.integration.endpoint.EventDrivenConsumer] to required type [org.springframework.messaging.MessageChannel] for property 'outputChannel': no matching editors or conversion strategy found

既然你有<int-ftp:outbound-channel-adapter id="ftpOutbound" channel="ftpChannel"

您应该使用 <int:transformer id="testTransformer" input-channel="ftpInbound" channel bean 名称,而不是 id该适配器的 <int:transformerd="testTransformer" input-channel="ftpChannel"

关于java - 将 channel 链接在一起会删除 file_originalFile header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26772160/

相关文章:

java - Java 构造函数在调用时是否返回对象或对象的 ID?

spring-ws - 将 Spring Integration Web 服务网关与 JMS 结合使用

java - Spring Service Activator - 我们可以对 Java 中的两个不同输入 channel 使用相同的方法和 ref 吗?

java - Spring Batch和Spring Integration的集成问题-“未为端点定义轮询器”异常

java - Spring Integration - 如何处理 LoggingHandler 的异常

java - Mybatis 无 Getter 属性

java - 土耳其 ("Asia/Istanbul"或 "Europe/Istanbul"上的 JDK 时区问题)

java - 使用 Spring Boot 模拟配置不会获取属性文件

java - 关于Java ServerSocket接受: busy-wating?

java - 接口(interface)中的 protected 方法