Apache Camel:我可以在条件选择语句的 when 部分中放置多个语句吗? [英] Apache Camel: can I put multiple statements in the when part of the conditional choice statement?

查看:37
本文介绍了Apache Camel:我可以在条件选择语句的 when 部分中放置多个语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得以下类型的路由:

I would like to obtain the following kind of routing:

  1. 带有 XML 正文的 HTTP POST 消息进入 CAMEL
  2. 我存储了 XML 正文的一些参数
  3. 消息被路由到外部端点
  4. 外部端点(外部服务器)回复

-> 此时,我想检查来自外部端点的回复是否是包含等于 SUCCESS 的 XML 参数的 HTTP 200 OK.-> 如果是这样,那么我想使用一些存储的参数来构造一个新的 HTTP 消息(此时方法 = PUT)并将其发送到外部端点

-> at this moment, I would like to check whether the reply from the external endpoint is a HTTP 200 OK containing a XML parameter equal to SUCCESS. -> if so, then I would like to use some of the stored parameters to construct a new HTTP message (method = PUT this time) and send it out to an external endpoint

我目前遇到的问题如下:

Problem that I am currently having, is the following:

.choice()
 .when(simple("${in.headers.CamelHttpResponseCode} == 200"))
   // now I want do a few things, eg: check also the XML body via xpath
   // and change the message to be sent out (change Method to PUT, ...)
    .to("http://myserver.com")
 .otherwise()
   // if no 200 OK, I want the route to be stopped ... not sure how ?
.end()

问题:知道如何在 HTTP 响应代码为 200 OK 的情况下添加这些额外的语句吗?看起来 when 不允许我添加额外的语句......(我的 Eclipse IDE 出现错误).

Question: any idea how to add those extra statements in case the HTTP response code was 200 OK ? It looks like the when does not allow me to add extra statements ... (I got an error in my Eclipse IDE).

提前致谢.

注意:如果 200 OK 匹配到新端点",我是否必须路由消息,然后使用此新端点创建新的路由?例如:

Note: could it be that I have to route the message in case the 200 OK matches to a 'new endpoint' and then create a new from route with this new endpoint ? Eg:

.choice()
     .when(simple("${in.headers.CamelHttpResponseCode} == 200"))
        .to("mynewendpoint")
     .otherwise()
       // if no 200 OK, I want the route to be stopped ... not sure how ?
    .end();

 from("mynewendpoint").
  .setHeader(etc etc)
  .to("http://myserver.com")

在后一种情况下,我应该如何准确定义这个新端点"?

In this latter case, how exactly should I define this 'newendpoint' ?

推荐答案

在 Java 等编程语言 DSL 中,您可以一起构建谓词.几年前我发布了一篇关于此的博客文章:http://davsclaus.blogspot.com/2009/02/apache-camel-and-using-compound.html

In the programming language DSLs such as Java, you can build predicates together. I posted a blog entry some years ago about this at: http://davsclaus.blogspot.com/2009/02/apache-camel-and-using-compound.html

例如有两个谓词

Predicate p1 = header("hl7.msh.messageType").isEqualTo("ORM"):
Predicate p2 = header("hl7.msh.triggerEvent").isEqualTo("001");

您可以使用 and 或 or 将它们链接在一起.

You can chain them together, using and or or.

Predicate isOrm = PredicateBuilder.and(p1, p2);

然后你就可以在路由中使用 isOrm

And then you can use isOrm in the route

from("hl7listener")
    .unmarshal(hl7format)
    .choice()
        .when(isOrm).beanRef("hl7handler", "handleORM")
        .otherwise().beanRef("hl7handler", "badMessage")
    .end()
    .marshal(hl7format);

这篇关于Apache Camel:我可以在条件选择语句的 when 部分中放置多个语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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