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

坚强是说给别人听的谎言 提交于 2020-01-11 07:24:08

问题


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:

Route 1:

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

Route 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.


回答1:


You can achieve "dynamic from" with Content Enricher pattern.

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}");


来源:https://stackoverflow.com/questions/36948005/how-do-dynamic-from-endpoints-and-exchanges-work-in-camel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!