【问题标题】:Apache Camel: Download Multiple Files at once using SFTP componentApache Camel:使用 SFTP 组件一次下载多个文件
【发布时间】:2020-03-20 20:00:27
【问题描述】:

在 sftp 上,我有几个具有以下 xyz 名称的文件:

40_20200313_0cd6963f-bf5b-4eb0-b310-255a23ed778e_p.dat
123_20200313_0cd6963f-bf5b-4eb0-b310-255a23ed778e_p.dat
etc.

我希望骆驼一次下载所有文件,因为目前它正在一个一个下载文件。

以下是骆驼路线和查询:

    private static String regex() {
        return "(22|23|24|25|26|28|29|32|35|40|41|46|52|70|85|88|123)_(?:.*)_p.dat";
    }

    private static String sftpComponent() {
        return "sftp://transit.ergogroup.no/Eyeshare/From_Eyeshare_Test"
                + "?username=Eyeshare_test"
                + "&password=epw3ePOugG" // Stored on wildfly server
                + "&download=true" //Shall be read chunk by chunk to avoid heap space issues. Earlier download=true was used: Harpreet
                + "&useList=true"
                + "&stepwise=false"
                + "&disconnect=true"
                + "&passiveMode=true"
                + "&reconnectDelay=10000"
//              + "&bridgeErrorHandler=true"
                + "&delay=300000"
                //+ "&fileName=" + sftpFileName
//              + "&include=kiki\\.txt"
//              + "&include=40_*_p\\.dat"sss
                + "&include="+regex()
                + "&preMove=$simple{file:onlyname}.$simple{date:now:yyyy-MM-dd'T'hh-mm-ss}.processing"
                + "&move=$simple{file:onlyname.noext}.$simple{date:now:yyyy-MM-dd'T'hh-mm-ss}.success"
                + "&moveFailed=$simple{file:onlyname.noext}.$simple{date:now:yyyy-MM-dd'T'hh-mm-ss}.failed";
//              + "&idempotentRepository=#infinispan"
//              + "&readLockRemoveOnCommit=true";
    }

   from(sftpComponent()).log("CHU").to(archiveReceivedFile())

代码看起来很好,但输出却不是。任何人都好心建议

【问题讨论】:

  • 为什么你问了很多问题却忽略了答案?如果其他人遇到同样的问题并找到您的问题怎么办?
  • @c0ld:不是这样的。你可以从一开始就检查我的整个历史。我正在接受答案,尝试一些事情,有些事情被搁置了,也正在给出我的答案。我怎样才能接受不必要的答案并混淆他人?请求您完全感谢
  • 我打开你的个人资料,最后 10 个问题是白色的 =) 是的,其中一些没有答案,但可以帮助 cmets。好的,这是你的决定。让我们回到问题。考虑到名称 archiveReceivedFile() 您想从已使用的一批文件中创建存档吗?只需要了解更多关于案例的信息,因为同时消耗未知数量的文件并不是一个好主意。
  • @c0ld:你的理解是正确的。好的,那我要一个一个来吃?

标签: java apache-camel camel-ftp


【解决方案1】:

这里有一些aggregator的例子:

from("file:///somePath/consume/?maxMessagesPerPoll=2&delay=5000")
            .aggregate(constant(true), new ZipAggregationStrategy()).completion(exchange -> exchange.getProperty("CamelBatchComplete", Boolean.class))
            .to("file:///somePath/produce/")

这里 maxMessagesPerPoll 定义将归档多少文件。但是,如果文件夹中的数量低于 maxMessagesPerPoll 值,它将等待丢失的文件以完成存档。这里是 ZipAggregationStrategy 的例子:

private static class ZipAggregationStrategy implements AggregationStrategy {
    private ZipOutputStream zipOutputStream;
    private ByteArrayOutputStream out;
    @Override
    public Exchange aggregate(final Exchange oldExchange, final Exchange newExchange) {
        try {
            if (oldExchange == null) {
                out = new ByteArrayOutputStream();
                zipOutputStream = new ZipOutputStream(out);
            }
            createEntry(newExchange);
            return newExchange;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private void createEntry(final Exchange exchange) throws Exception {
        final ZipEntry zipEntry = new ZipEntry(exchange.getIn().getHeader(Exchange.FILE_NAME, String.class));
        zipOutputStream.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        int length;
        try (InputStream body = exchange.getIn().getBody(InputStream.class)) {
            while ((length = body.read(bytes)) >= 0) {
                zipOutputStream.write(bytes, 0, length);
            }
        }
    }
    @Override
    public void onCompletion(final Exchange exchange) {
        try {
            zipOutputStream.close();
            exchange.getIn().setBody(new ByteArrayInputStream(out.toByteArray()));
            exchange.getIn().setHeader(Exchange.FILE_NAME, "someArchive.zip");
        }catch (Exception e){
            throw new RuntimeException(e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这是内存中的示例。例如,您可以使用临时文件对其进行改进。而且您始终可以根据您的逻辑创建自己的完成谓词。

UPD:我认为文档链接暂时不可用

【讨论】:

  • 对不起,但我不明白这个答案与问题有什么关系。在我看来,问题是“如何同时从 SFTP 下载文件”,但这个答案是关于如何归档一批文件或类似的东西。
猜你喜欢
  • 1970-01-01
  • 2018-06-25
  • 2019-12-22
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
相关资源
最近更新 更多