2014-02-20 62 views
1

我想設置屬性isEven在Exchange.property,然後使用選擇來評估它的路線。
該物業正在設置,但我總是得到,否則結果(NACK)無論什麼是EVE設置。駱駝交易所屬性不用簡單的XML在評估

這裏是我設置:

// Below is used for development 
// If the property.isEven == true then an ACK will be returned from the Mock HRM 
// If false then NACK 

    int lastDigit = Integer.parseInt(exchange.getExchangeId().substring(exchange.getExchangeId().length() - 1)); 

    // check if lastDigit is odd or even 
    if ((lastDigit & 1) == 0) 
    { 
     exchange.setProperty("isEven", Boolean.TRUE); 
     System.out.println("\n\n\n********** Exchange Id lastDigit " + lastDigit + " isEven: " + exchange.getProperty("isEven") + " ***********"); 

    } 
    else 
    { 
     exchange.setProperty("isEven", Boolean.FALSE); 
     System.out.println("\n\n\n********** Exchange Id lastDigit " + lastDigit + " isEven: " + exchange.getProperty("isEven") + " ***********"); 

    } 

中的println的節目,我設置ISEVEN我期望的那樣。 這裏是路線:

<!-- Used to ramdomly send Ack or Nack --> 
<log message="isEven property is :: ${property[isEven]}" /> 
    <camel:choice> 
     <camel:when> 
      <simple>"${property[isEven]}"</simple> 
       <transform> 
      <constant>ACK</constant> 
      </transform>  
     </camel:when> 
     <camel:otherwise> 
      <transform> 
      <constant>NACK</constant> 
     </transform>  
     </camel:otherwise> 
    </camel:choice> 

日誌消息從未計算表達式$ {屬性[ISEVEN]} 這裏是輸出 日誌[ISEVEN屬性是:: $ {屬性[ISEVEN]}]

如果我更改簡單表達式來明確檢查是否真的我總是得到ACK無論屬性設置爲什麼。

<simple>"${property[isEven]} == true"</simple> 

我已經搜索了網頁,但找不到很多使用簡單和Exchange屬性的示例。

任何人都可以看到我錯過了什麼?

感謝,

安德魯

彼得表明,他能做到這一點很容易我試過他的例子,我還沒有嘗試過2之後。這是一個。它不適合我。它生成了Hello :: NACK,無論isEven是true還是false:

<camel:choice> 
      <camel:when> 
       <simple>${property[isEven]} == "true"</simple> 
       <log message="HELLO :: ACK" /> 
       <!-- <transform> 
        <constant>ACK</constant> 
       </transform> -->  
      </camel:when> 
      <camel:otherwise> 
       <log message="HELLO :: NACK" /> 
       <!-- <transform> 
        <constant>NACK</constant> 
       </transform> -->  
      </camel:otherwise> 
     </camel:choice> 

這是一件有趣的事情。它看起來像下面的日誌是說其空在最後像

********** Exchange Id lastDigit 2 isEven: true *********** 

14/02/20 14:09:13 INFO interceptor.Tracer: >>> (toHRMRoute) bean://hl7handler?method=handleORM --> log[isEven property is :: ${property[isEven]}] <<< Pattern:InOut, Properties {CamelToEndpoint=bean://hl7handler?method=handleORM, CamelMessageHistory [DefaultMessageHistory[routeId=toHRMRoute, node=to3], DefaultMessage History[routeId=toHRMRoute, node=log1]], CamelCreatedTimestamp=Thu Feb 20 14:09:13 CST 2014} 

14/02/20 14:09:13 INFO toHRMRoute: ** isEven property is :: ** 

我覺得彼得是,它必須是我有我的路線設置方式正確。

<endpoint id="hrmMockHL7Listener" 
     uri="netty:tcp://localhost:9200?sync=true" /> 
<!-- Sending data using postman to a rest server--> 
<route id="pushRESTRoute"> 
<from uri="cxfrs://bean://pushRESTServer" /> 

    <!-- this process is where we set isEven on the Exchange--> 
<process ref="transformer"/> 
    <!-- Send it to a tcp listener at port 9200--> 
<to ref="hrmMockHL7Listener" /> 
</route> 
<!-- Changed routes does the Exchange keep properties? --> 
<route id="toMRoute"> 
<from uri="hrmMockHL7Listener" /> 
<to uri="bean:hl7handler?method=handleORM" /> 
<!-- Used to ramdomly send Ack or Nack --> 
<log message="isEven property is :: ${property[isEven]}"> 
    // see the beginning of the question for choice code. 

望着輸出似乎ISEVEN屬性被丟棄的路線之間:

14/02/21 09:37:26 INFO interceptor.Tracer: >>> (pushRESTRoute) ref:transformer --> tcp://localhost:9200 <<< Pattern:InOut, Properties {CamelMessageHistory=[DefaultMessageHistory[routeId=pushRESTRoute, node=process1], DefaultMessageHistory[routeId=pushRESTRoute, node=to1]], CamelCreatedTimestamp=Fri Feb 21 09:37:26 CST 2014, isEven=true}  

見ISEVEN在結束了嗎?自帶接下來的示蹤劑沒有它

/02/21 09:37:26 INFO interceptor.Tracer: >>> (toMRoute) from(tcp://localhost:9200) --> bean://hl7handler?method=handleORM <<< Pattern:InOut, Properties:{CamellMessageHistory=[DefaultMessageHistory[routeId=toMRoute, node=to3]], CamelCreatedTimestamp=Fri Feb 21 09:37:26 CST 2014} 

從交易所的javadoc

An Exchange is the message container holding the information during the entire routing of a Message received by a Consumer. 

是否整個包括跨型動物路線?

+0

你使用什麼版本的駱駝? –

+0

我正在使用2.12.2 – KingAndrew

回答

0

您不需要使用引號。儘量只

<simple>${property.isEven} == true</simple> 
+0

這就是我以前的做法。我改回來了。確保我保存並重新編譯。 :)並再次嘗試。沒有運氣。我每次都得到NACK(否則)。爲了記錄,isEven每次都會改變。 lastDigit從2開始,然後7重複。 – KingAndrew

1

爲了測試我改變了路線一點點:

<route id="startRoute"> 
    <from uri="direct:start" /> 
    <multicast stopOnException="true"> 
     <to uri="direct:trigger" /> 
     <to uri="direct:trigger" /> 
     <to uri="direct:trigger" /> 
    </multicast> 
</route> 

<route> 
    <from uri="direct:trigger" /> 
    <process ref="myProcessor" /> 
    <log message="isEven property is :: ${property[isEven]}" /> 
    <camel:choice> 
     <camel:when> 
      <simple>"${property.isEven}"</simple> 
      <log message="HELLO :: ACK" /> 
     </camel:when> 
     <camel:otherwise> 
      <log message="HELLO :: NACK" /> 
     </camel:otherwise> 
    </camel:choice> 
</route> 

<!-- scope singleton is default --> 
<bean id="myProcessor" class="ch.keller.test.testcamelspring.util.Trigger" scope="singleton" /> 

處理器定義如下:

public class Trigger implements Processor { 
    @Override 
    public void process(final Exchange exchange) throws Exception { 
     // your code comes here 
    } 
} 

對於我來說,下面的表達式和預期一樣:

<simple>"${property[isEven]}"</simple> 

<simple>${property[isEven]}</simple> 

<simple>${property[isEven]} == "true"</simple> 

<simple>"${property.isEven}"</simple> 

<simple>${property.isEven} == "true"</simple> 

<simple>${property.isEven}</simple> 

我更喜歡上一個版本。

編輯:

爲了調試如果該屬性設置是否正確,在Spring配置文件中啓用showProperties

<bean id="traceFormatter" class="org.apache.camel.processor.interceptor.DefaultTraceFormatter"> 
    <property name="showBreadCrumb" value="false" /> 
    <property name="showProperties" value="true" /> 
</bean> 

那麼你應該看到在日誌下面的輸出(縮短更好的可讀性):

[main] Tracer INFO >>> (route1) log[isEven property is :: ${property[isEven]}] --> choice <<< Pattern:InOnly, Properties:{CamelToEndpoint=direct://trigger, ..., isEven=true, ... 

重要部分是isEven=true

編輯:

轉發到可證明是另一條路線時,該物業保持如下:

<route> 
    <from uri="direct:trigger" /> 
    <process ref="myProcessor" /> 
    <log message="isEven property is :: ${property[isEven]}" /> 
    <to uri="direct:acktarget" /> 
</route> 

<route> 
    <from uri="direct:acktarget" /> 
    <log message="acktarget: isEven property is :: ${property[isEven]}" /> 
</route> 

輸出:

exchange.getExchangeId() = ID-pis-iMac-local-54434-1393006139076-0-3 
********** Exchange Id lastDigit 3 isEven: false *********** 
[       main] route1       INFO isEven property is :: false 
[       main] route2       INFO acktarget: isEven property is :: false 
exchange.getExchangeId() = ID-pis-iMac-local-54434-1393006139076-0-4 
********** Exchange Id lastDigit 4 isEven: true *********** 
[       main] route1       INFO isEven property is :: true 
[       main] route2       INFO acktarget: isEven property is :: true 
exchange.getExchangeId() = ID-pis-iMac-local-54434-1393006139076-0-5 
********** Exchange Id lastDigit 5 isEven: false *********** 
[       main] route1       INFO isEven property is :: false 
[       main] route2       INFO acktarget: isEven property  is :: false 

即使我調用一個bean在將消息轉發到其他路由之前,該屬性將保留。所以,我想你的問題在<to uri="bean:hl7handler?method=handleORM" />。嘗試在調用此bean之前記錄屬性,並查看屬性是否仍然設置。如果沒有,看看這個bean。

+0

哇。現在你只是讓我看起來很糟糕。 ;-)我會放入這些日誌並嘗試一些成功的例子。 – KingAndrew

+0

彼得是你用布爾。實踐和布爾。錯誤?如果你的簡單表達式不適合我,也許它是如何設置它們的? – KingAndrew

+0

我實際上使用過你的處理器。但也許它必須做)a)如何調用你的路由(見上面我的增強示例),b)如果你的'myProcessor'具有'singelton'(默認)或'prototype'作用域.. –