2013-10-07 41 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; 
     } 
    } 
} 

谢谢克劳斯指着骆驼环境