简体   繁体   English

动态“来自”端点和交换在骆驼中如何工作?

[英]How do dynamic “from” endpoints and exchanges work in camel?

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. 因此,假设我有一条包含交换数据的路由,然后我想在“ from”端点中的另一条路由中使用来自交换的标头。

I think it would look something like this: 我认为它看起来像这样:

Route 1: 路线1:

from("file:/dir1")
...
.to ("direct:start");

Route 2: 路线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. 因此,这些步骤对我来说似乎是正确的,但我感觉我有点像在污染这种交流。

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. 我没有在骆驼包中搜索一个。

You can achieve "dynamic from" with Content Enricher pattern. 您可以使用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}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM