Apache Camel: how store variable for later use

opstalj picture opstalj · Feb 8, 2012 · Viewed 43.2k times · Source

while 'playing around' with Camel using Spring DSL, I came across the following problem. Suppose the expected message flow looks like this:

  1. client sends HTTP POST message with XML body to CAMEL
  2. CAMEL proxies HTTP POST message towards server, with the URI slightly adapted using info from the received XML body (eg: use XPATH to filter out a certain parameter)
  3. after CAMEL has received a reply, CAMEL sends HTTP PUT message towards server, using parameters out of the XML body received in 1

So something like:

<route>
   <from uri="...">
   <to uri="...">
   <to uri="...">
 </route>

Question: how do I store the parameters in Spring DSL in step 1, so that I can use them later in step 3 ?

So, I would like to extract XML parameters out of the XML body of the message received in step 1 and put them into variables, which I then later on can use to compose the message to be sent in step 3.

For extracting the parameters, I was thinking of using XPATH. That looks ok, but I just don't see how to put the output of the XPATH into a variable and then use that variable later on ... (syntax ??)

Note: as you can see, my development knowledge is rather limited ... sorry for that. But it would still be great if someone could help with this :).

Answer

Ben ODay picture Ben ODay · Feb 8, 2012

you can set store data in the Exchange properties or message headers like this...

.setHeader("ID", XPathBuilder.xpath("/order/@id", String.class))
.setProperty("ID", XPathBuilder.xpath("/order/@id", String.class))

and then retrieve them in a bean/processor from the Exchange like this...

String propId = (String) exchange.getProperty("ID");
String headerId = (String) exchange.getIn().getHeader("ID");                        }