spring Apache Camel:如何存储变量以备后用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9194720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Camel: how store variable for later use
提问by opstalj
while 'playing around' with Camel using Spring DSL, I came across the following problem. Suppose the expected message flow looks like this:
在使用 Spring DSL 与 Camel“玩耍”时,我遇到了以下问题。假设预期的消息流如下所示:
- client sends HTTP POST message with XML body to CAMEL
- 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)
- after CAMEL has received a reply, CAMEL sends HTTP PUT message towards server, using parameters out of the XML body received in 1
- 客户端将带有 XML 正文的 HTTP POST 消息发送到 CAMEL
- CAMEL 向服务器代理 HTTP POST 消息,URI 使用接收到的 XML 正文中的信息稍作调整(例如:使用 XPATH 过滤掉某个参数)
- CAMEL 收到回复后,CAMEL 使用 1 中收到的 XML 正文中的参数向服务器发送 HTTP PUT 消息
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 ?
问题:如何将步骤 1 中的参数存储在 Spring DSL 中,以便稍后在步骤 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.
因此,我想从步骤 1 中收到的消息的 XML 正文中提取 XML 参数并将它们放入变量中,然后我可以使用这些变量来编写要在步骤 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 ??)
为了提取参数,我在考虑使用 XPATH。看起来不错,但我只是不知道如何将 XPATH 的输出放入变量中,然后稍后使用该变量......(语法??)
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 :).
注意:如您所见,我的开发知识相当有限……抱歉。但是,如果有人可以帮助解决这个问题,那就太好了:)。
回答by Ben ODay
you can set store data in the Exchangeproperties or message headers like this...
您可以像这样在Exchange属性或邮件标题中设置存储数据...
.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...
然后像这样从 Exchange 在 bean/处理器中检索它们......
String propId = (String) exchange.getProperty("ID");
String headerId = (String) exchange.getIn().getHeader("ID"); }
回答by Juan Pablo Astorga
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
检查简单的表达语言:http: //camel.apache.org/simple.html
Sometimes looking at the test cases of Camel can be helpful as well, in particular for Spring DSL:
有时查看 Camel 的测试用例也会有所帮助,尤其是对于 Spring DSL:

