Apache Camel:如何存储变量以备后用 [英] Apache Camel: how store variable for later use

查看:27
本文介绍了Apache Camel:如何存储变量以备后用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Spring DSL 与 Camel 一起玩耍"时,我遇到了以下问题.假设预期的消息流如下所示:

  1. 客户端向 CAMEL 发送带有 XML 正文的 HTTP POST 消息
  2. CAMEL 代理发往服务器的 HTTP POST 消息,URI 稍作调整,使用来自接收到的 XML 正文的信息(例如:使用 XPATH 过滤掉某个参数)
  3. 在 CAMEL 收到回复后,CAMEL 使用 1 中收到的 XML 正文中的参数向服务器发送 HTTP PUT 消息

比如:

<来自 uri="..."><to uri="..."><to uri="..."></路线>

问题:如何将步骤 1 中的参数存储在 Spring DSL 中,以便稍后在步骤 3 中使用它们?

因此,我想从步骤 1 中收到的消息的 XML 正文中提取 XML 参数,并将它们放入变量中,然后我可以使用这些变量来编写要在步骤 3 中发送的消息.

为了提取参数,我考虑使用XPATH.看起来不错,但我只是不知道如何将 XPATH 的输出放入变量中,然后稍后使用该变量......(语法??)

注意:如您所见,我的开发知识相当有限……抱歉.但如果有人能帮忙解决这个问题,那就太好了:)

解决方案

你可以在Exchange 属性或这样的消息标题...

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

然后像这样在一个 bean/处理器中从 Exchange 中检索它们......

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

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

解决方案

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

这篇关于Apache Camel:如何存储变量以备后用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆