简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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