2012-07-27 155 views
7

我有一個有兩個操作的服務。Apache Camel有條件路由

RegisterUser 
UpdateUser 

我有般地擊潰:

<camel:route id="myRoute"> 
    <camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" />    
    <camel:bean ref="processor" method="processMessage"/> 
    <camel:to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/> 
    <camel:to uri="cxf:bean:myTargetEndpoint"/> 
</camel:route> 

在我的處理器豆,當我指定:

RegisterUser registerUser = exchange.getIn().getBody(RegisterUser.class); 

我得到的註冊用戶對象。一切正常。 的問題是,我想駱駝路由我的要求條件,爲如:

如果服務操作RegisterUser我想將消息路由到我的指定bean的,如果服務操作UpdateUser我想將消息路由到另一個bean。

我試過使用駱駝xPath,但它似乎並沒有工作。

<camel:route id="myRoute"> 
    <camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" /> 
    <camel:choice> 
     <camel:when> 
      <camel:xpath> 
       //RegisterUser 
      </camel:xpath> 
      <camel:bean ref="processor" method="processMessage"/> 
      <camel:to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/> 
     </camel:when> 
    </camel:choice>       
    <camel:to uri="cxf:bean:myTargetEndpoint"/> 
</camel:route> 

我在搜索如何設置駱駝路由到不同的目標,但沒有找到任何東西。也許有人知道哪裏可能是問題?

回答

14

將在郵件頭中所需要的操作的信息。

你正在尋找的頭被稱爲「operationName」

所以這裏有一個例子:

<camelContext xmlns="http://camel.apache.org/schema/blueprint"> 
    <route id="example"> 
     <from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" /> 
     <log message="The expected operation is :: ${headers.operationName}" /> 
     <choice> 
      <when> 
       <simple>${headers.operationName} == 'RegisterUser'</simple> 
        <bean ref="processor" method="processMessage"/> 
       <to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/> 
      </when> 
      <when> 
       <simple>${headers.operationName} == 'UpdateUser'</simple> 
       <!-- Do the update user logic here --> 
       <bean ref="processor" method="updateUser" /> 
      </when> 
     </choice> 
    <to uri="cxf:bean:myTargetEndpoint"/> 
    </route> 
</camelContext> 

(注意使用Apache的白羊座藍圖的例子 - 但是這將是春天相同,除了命名空間)

+0

這對我很好。正是我需要的。謝謝! :) – 2012-07-31 07:57:14

4

嘗試使用camel-simple表達式代替的XPath這個...

<when><simple>${body} is 'com.RegisterUser'</simple><to uri="..."/></when>