2013-10-07 66 views
0

我試圖通過下面的try catch塊的幫助來查找是否存在駱駝直接路由。我正在尋找一個謂詞來檢查路線是否存在於駱駝中。我無法找到任何東西,直接給我的答案,所以我採取了以下做法,如何檢查apache駱駝路線是否存在?

<doTry> 
    <recipientList> 
     <description>Check if a country specific handler is available</description> 
     <simple>direct:${header.operationName}${body.country}</simple> 
    </recipientList> 
    <doCatch> 
     <exception>org.apache.camel.component.direct.DirectConsumerNotAvailableException</exception> 
     <recipientList> 
      <description>if a country specific handler is not available to to the base</description> 
      <simple>direct:${header.operationName}</simple> 
     </recipientList> 
    </doCatch> 
</doTry> 

這意味着我不得不使用異常處理程序中的駱駝趕着DirectConsumerNotAvailableException,以確定是否路由存在。我期待在另一種方法,我們可以用一個簡單的語句,比如存在以下

<choice> 
    <when> 
     <description>Check if a country specific handler is available</description> 
     <simple>direct:${header.operationName}${body.country} exists</simple> 
     <recipientList> 
      <description>country specific handler is available</description> 
      <simple>direct:${header.operationName}${body.country}</simple> 
     </recipientList> 
    </when> 
    <otherwise> 
     <recipientList> 
      <description>country specific handler is not available then route to generic processing</description> 
      <simple>direct:${header.operationName}</simple> 
     </recipientList> 
    </otherwise> 
</choice> 

請讓我知道是否可以使用一些其他方式來實現這樣的事情。

回答

1

如駱駝2.11.x的,簡單的表達有一個稱爲camelContext新的變量,其可以用來在路徑存在camelContext.hasEndpoint來評價。

<choice> 
    <when> 
     <description>Check if a country specific handler is available</description> 
     <simple>${camelContext.hasEndpoint(direct:${header.operationName}${body[0].country})} != null</simple> 
     <recipientList> 
      <description>country specific handler is available</description> 
      <simple>direct:${header.operationName}${body.country}</simple> 
     </recipientList> 
    </when> 
    <otherwise> 
     <recipientList> 
      <description>country specific handler is not available then route to generic processing</description> 
      <simple>direct:${header.operationName}</simple> 
     </recipientList> 
    </otherwise> 
</choice> 

對於使用像2.10.x Apache的駱駝的早期版本的,你可以使用駱駝的上下文檢查,如果通過交換對象及其動態路由器

路由信息存在的路線,

<dynamicRouter> 
    <method ref="dynamicRouterService" method="route"/> 
</dynamicRouter> 

動態路由器,

public class DynamicRouterService { 
    public String route(@Header("operationName") String operationName, @Properties Map<String, Object> properties, Exchange exchange, Country body) { 
     //needed to end the iterative routing calls 
     if (Boolean.valueOf((String)properties.get("SERVICE_INVOKED"))) { 
      return null; 
     } 

     //indicate that the route has been invoked 
     properties.put("SERVICE_INVOKED", "true"); 

     return this.orchestrateRoute(exchange, operationName, body.getcountry()); 
    } 
    private String orchestrateRoute(Exchange exchange, String operationName, String country) { 
     String uriBaseString = "direct:" + operationName; 
     String uriCountryString = uriBaseString + country; 
     Endpoint endpoint = exchange.getContext().hasEndpoint(uriCountryString); 
     if (endpoint != null) { 
      return uriCountryString; 
     } else { 
      return uriBaseString; 
     } 
    } 
} 

謝謝克勞斯指着駱駝環境