2012-02-10 81 views
7

我想獲得下列一種路由的:Apache Camel:我可以在條件選擇語句的部分時間放置多個語句嗎?

  1. 與XML主體HTTP POST消息進入CAME​​L
  2. 我存儲一些
  3. 該消息路由到外部XML主體的參數的端點
  4. 外部端點(外部服務器)回覆

- >在這一刻,我想檢查從外部端點的回覆是否是HTTP 20 0 OK包含一個等於SUCCESS的XML參數。如果是這樣,那麼我想使用一些存儲的參數來構造一個新的HTTP消息(方法= PUT這次)並將它發送到外部端點

問題,我目前有,是如下:

.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?它看起來像什麼時候不允許我添加額外的語句... (我的Eclipse IDE中出現錯誤)。

在此先感謝。

注意:如果200 OK匹配到「新端點」,然後使用此新端點創建一條新的路由,是否可能需要路由該消息? 如:

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

在後一種情況下,我究竟應該定義這個 'newendpoint'?

回答

20

在諸如Java的編程語言DSL中,您可以一起構建謂詞。我在發表了一篇博客條目幾年前這個問題: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。

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

然後你就可以在路由

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

超級!謝謝。 – opstalj 2012-02-14 08:35:16

+0

爲什麼'Predicate isOrm = PredicateBuilder.and(header(「hl7.msh.messageType」)isEqualTo(「ORM」),header(「hl7.msh.triggerEvent」)isEqualTo(「001」));''不會工作?關於如何以及何時對它進行評估? – Antares42 2015-01-21 14:26:35

+0

有沒有辦法在藍圖中使用這個PredicateBuilder? – Yannick 2017-01-30 07:53:59

4

是的,你可以有)。否則(對。當與多條語句(及),您可以隨時撥打.endChoice()顯式結束每個條件塊......

到您的其他問題,你可以使用camel-direct將多條路由等鏈接在一起...

+0

由於使用isOrm。我之前必須做出語法錯誤,我確實可以在.when()和.otherwise()之間輸入多個語句。 – opstalj 2012-02-13 07:44:12