gpt4 book ai didi

java - Apache Camel : using stream and onCompletion

转载 作者:行者123 更新时间:2023-11-29 05:36:08 24 4
gpt4 key购买 nike

我有一个路由需要通过 HTTP 下载一个文件。

当收到包含命令的消息(包含 HTTP 资源的 URL,以及保存它的本地文件的 URI)时动态添加此路由,然后当文件传输完成时我想删除该路由,因此仅使用 Camel 作为协议(protocol)翻译器。

我为此使用 Camel,因为这条路线属于一个基于 EIP 构建的项目,其中到处都使用 Camel 进行消息传递和集成。

我用过:

onCompletion().setBody(simple("")).bean(new Stop(getContext(), transferID));

from("stream:url?url="+from).to("stream:file?fileName="+to).routeId(this.transferID);

(文件很大,10-100 Gb)

我看到 onCompletion 在整个文件传输之前触发,实际上它只传输不可预测数量的字节,比如 100-300 kB。

“流”和 onCompletion 不兼容还是我做错了什么?

编辑:在我正在做的 Bean 中:

c.stopRoute(transferID);
c.removeRoute(transferID);

最佳答案

stream component 将为文件的每一行创建一个新的交换

流组件提供了一个选项来拆分 XX 行(默认为 0):

groupLines 0 Camel 2.5: To group X number of lines in the consumer. For example to group 10 lines and therefore only spit out an Exchange with 10 lines, instead of 1 Exchange per line.

此选项用于创建 XX 行的新交换。这是StreamConsumer的相关代码:

 if (lines.size() >= endpoint.getGroupLines()) {
// spit out lines
Exchange exchange = endpoint.createExchange();

// create message with the lines
Message msg = new DefaultMessage();
List<String> copy = new ArrayList<String>(lines);
msg.setBody(endpoint.getGroupStrategy().groupLines(copy));
exchange.setIn(msg);

// clear lines
lines.clear();

getProcessor().process(exchange);
}

onCompletion 在每次交换完成后执行

来自 Camel 文档(onCompletion documentation):

triggered either always, only if completed with success, or only if failed

因此您的代码在读取第一行后停止路由

显然实际上没有办法知道流组件何时到达文件末尾。

一个不错的方法可能是在 splitter 中拥有一个 CamelSplitComplete 属性.我们可以这样使用(参见 onCompletion with onWhen predicate 章):

onCompletion()
.onWhen(property("CamelSplitComplete").isEqualTo("true"))
.setBody(simple("")).bean(new Stop(getContext(), transferID));

编辑: 我只是在寻找流消费者,但因为您也在使用生产者。查看 closeOnDone 属性,它可能会起作用。

closeOnDone | false | Camel 2.11.0: This option is used in combination with Splitter and streaming to the same file. The idea is to keep the stream open and only close when the Splitter is done, to improve performance. Mind this requires that you only stream to the same file, and not 2 or more files.

关于java - Apache Camel : using stream and onCompletion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19494791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com