动态“从"怎么做?端点和交换在骆驼中工作?
我有点纠结于动态路由概念和消费者规则.
I'm sort of struggling with the dynamic routing concept and consumer rules.
假设我有一个包含交换数据的路由,然后我想在来自"端点的不同路由中使用来自交换的标头.
So let's say I have a route with exchange data, and then I want to use a header from the exchange in a different route in the "from" endpoint.
我认为它看起来像这样:
I think it would look something like this:
路线 1:
from("file:/dir1")
...
.to ("direct:start");
路线 2:
from("direct: start")//get the old exchange data
.from("file:/dir1/?fileName=${header.myHeader}")//start consuming from a different endpoint using old exchange data
...
.to("direct: end);
所以这些步骤对我来说似乎是正确的,但我觉得我有点污染了交易所.
So those steps seems right to me, but I feel like Im sort of polluting the exchange.
对我来说,我使用的是动态路由,但同时我也创建了一个新的消费者.这意味着我正在创建一个新的交易所,对吧?那么,camel 如何知道在其余路线中选择和使用哪个交易所?
To me, Im using dynamic routing but Im also creating a new consumer at the same time. That means Im creating a new exchange right? So, how does camel know which exchange to pick and use in the rest of the route?
起初我认为它可能结合了它们,但我做了更多的挖掘,发现你实际上需要使用丰富"来添加到现有的交换.
At first I thought it probably combined them, but I did a bit more digging and found that you actually need to use "enrich" to add to an existing exchange.
有人能解释一下骆驼是如何处理这种情况的吗?如果你有一个很好的例子.我在骆驼包里找了一个,没有成功.
Can someone explain how camel handles this sort of scenario? If you have an example that would be great too. I searched for one in the camel package with no success.
推荐答案
你可以通过 Content Enricher 模式.
假设您的第一个路由用于将文件名添加到标题中,例如:
Let's say your first route is used to add file name to the header for instance like this:
from("timer:trigger?repeatCount=1")
.routeId("define-file-name")
.setHeader("myHeader", constant("file.txt"))
.to("direct:start");
然后您的第二条路由可以使用来自交换标头的信息来轮询该文件,如下所示.
Then your second route can poll for that file using the information from the exchange header like this.
from("direct:start")
.routeId("poll-file")
.pollEnrich().simple("file://dir1?fileName=${in.header.myHeader}").timeout(10000)
.log("${body}");
相关文章