Apache Camel: can I put multiple statements in the when part of the conditional choice statement?

杀马特。学长 韩版系。学妹 提交于 2019-12-08 15:44:54

问题


I would like to obtain the following kind of routing:

  1. HTTP POST message with XML body enters CAMEL
  2. I store some of the parameters of the XML body
  3. The message is routed to an external endpoint
  4. The external endpoint (external server) replies

-> 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()

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

Thanks in advance.

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' ?


回答1:


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

For example having two predicates

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

You can chain them together, using and or or.

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

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



回答2:


yep, you can have multiple statements between the .when() and .otherwise() and you can always call .endChoice() to explicitly end each conditional block...

to your other question, you can use camel-direct to chain together multiple routes, etc...



来源:https://stackoverflow.com/questions/9227837/apache-camel-can-i-put-multiple-statements-in-the-when-part-of-the-conditional

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!