如何检查apache camel route是否存在?

时间:2013-10-07 10:42:26

标签: apache-camel routes exists predicate

我试图在下面的try catch块的帮助下找到驼峰中是否存在直接路由。我正在寻找一个谓词来检查骆驼中是否存在路线。我找不到任何直接给我答案的东西,所以我采取了以下方法,

<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>

这意味着我被迫使用camel中的异常处理程序来捕获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>

如果可以通过其他方式实现这样的目的,请告诉我。

2 个答案:

答案 0 :(得分:1)

从camel 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>

对于使用早期版本的apache-camel(如 2.10.x )的用户,可以使用camel的上下文来检查路由是否存在通过交换对象及其动态路由器

路线信息,

<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;
        }
    }
}

感谢克劳斯指出骆驼的背景

答案 1 :(得分:0)

请参阅此常见问题解答,了解动态&lt;到&gt; http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html

然后,您可以在bean上使用方法来计算端点。并且CamelContext上有API来检查路由/端点是否存在。