【问题标题】:Apache Camel and FTP processingApache Camel 和 FTP 处理
【发布时间】:2013-01-22 15:49:39
【问题描述】:

我们正在尝试在 Camel 中构建一个进程服务器。我们有几个客户端,通过 FTP 提供文件。我们必须镜像 ftp 文件,但我们只想下载我们还没有的文件。客户端的服务器必须保持不变,因此不允许更改,只能读取。

String to = DownloadFolder.getInstance().getDownloadFolder() + File.separator + "test";
                from("ftp://user@server/downloads/&binary=true&stepwise=false&localWorkDirectory=/tmp")//
                        .process(new ProcessCheckForDownload(to))//
                        .to("file://" + to + "?keepLastModified=true")//
                        .to("jms:queue:FTP_FILE_RECEIVED");

问题是,在我检查下载之前 ftp 正在下载文件。当设置download=false时,我可以检查下载,但是如何继续。至少现在我无法处理单个文件进行下载。下一个问题是,这项工作被破坏了,因为 jms 想要获取临时文件,而不是最终文件。

也许有人对如何解决这个问题有提示。

【问题讨论】:

    标签: file ftp download apache-camel


    【解决方案1】:

    文件和FTP组件都内置了这个功能。它被称为idempotent consumer。基本上可以在端点上通过idempotent=true开启这个功能。

    默认的幂等 repo 无法在服务器重新启动后继续存在,因为它会将有关已读取文件的数据存储在内存中。您可以改用持久性幂等存储库,例如 File based idempotent repository(或一些 database repos,或您的自定义类)。

    file 页面上有很多示例,因为 FTP 组件继承自 File,同样适用(大部分情况)。

    顺便说一句,download=false 功能仅适用于尚未发布的 Camel 2.11。

    【讨论】:

    • 不幸的是,功能 idempotent=true 实际上只检查文件名。当文件更新时,它无法识别这些更改。但这是我们的一个要求。
    • 是的,这是一个好主意,允许人们自定义密钥并指定包含时间戳等。我已经记录了一张票:issues.apache.org/jira/browse/CAMEL-6006
    • @ClausIbsen:我尝试将 indempotentKey 与 2.11-SNAPSHOT 和 sftp 方案一起使用,但骆驼抱怨未知参数。更改是否仅影响文件组件,还是我应该能够将 indempotentKey 与 sftp 一起使用?
    【解决方案2】:

    我认为我们必须扩展 GenericFileConsumer 的 isValidFile 方法。

    protected boolean isValidFile(GenericFile<T> file, boolean isDirectory) {
            if (!isMatched(file, isDirectory)) {
                log.trace("File did not match. Will skip this file: {}", file);
                return false;
            } else if (endpoint.isIdempotent() && endpoint.getIdempotentRepository()**.contains(file.getAbsoluteFilePath())**) {
                log.trace("This consumer is idempotent and the file has been consumed before. Will skip this file: {}", file);
                return false;
            }    
            // file matched
            return true;
        }
    

    我们必须生成我们自己的包含大小和上次更改日期的密钥。我认为这样可以解决问题。

    .contains(file.getAbsoluteFilePath()+":"+file.getFileLength()+":"+file.getLastModified())
    

    我们所要做的就是创建自己的消费者。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多