Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Camel: how store variable for later use

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 :).

like image 225
opstalj Avatar asked Feb 08 '12 13:02

opstalj


2 Answers

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");                        }
like image 96
Ben ODay Avatar answered Oct 18 '22 22:10

Ben ODay


I leave you some examples:

<setHeader headerName="token">
    <constant>someValue</constant>
</setHeader>

<setHeader headerName="userName">
    <simple>${properties:userName}</simple>  //from config
</setHeader>

<setProperty propertyName="bodyBkp">
    <simple>${in.body}</simple>
</setProperty>

<setProperty propertyName="orderNumber">
    <xpath resultType="String">//item[1]/orderNumber/text()</xpath>
</setProperty>

Getter

${exchangeProperty[orderNumber]}

${in.headers.token}

Documentation

Check the simple expression language: http://camel.apache.org/simple.html

Sometimes looking at the test cases of Camel can be helpful as well, in particular for Spring DSL:

  • setProperty with Spring DSL
  • setHeader using XPATH with Spring DSL
  • simple expression language test
like image 41
Juan Pablo Astorga Avatar answered Oct 19 '22 00:10

Juan Pablo Astorga