سؤال

I am trying to find if a direct route exists in camel with the help of a try catch block like below. I am looking for a predicate to check if a route exists in camel or not. I could not find anything that directly gives me the answer, so I took the below approach,

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

This means that I am forced to use the exception handler in camel to catch a DirectConsumerNotAvailableException to determine if a route exists. I am looking at an alternate approach where we can use a simple expression like exists below,

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

Please let me know if something like this can be achieved using some other means.

هل كانت مفيدة؟

المحلول

As of camel 2.11.x, the simple expression has a new variable called camelContext which can be used to evaluate if a route exists 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>

For those using an earlier version of apache-camel like 2.10.x, you can use camel's context to check if a route exists via the exchange object and its Dynamic router

Route info,

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

The Dynamic router,

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

Thanks Claus for pointing to the camel context

نصائح أخرى

See this FAQ about dynamic < to > http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html

You can then use a method on a bean to compute the endpoint. And there is API on CamelContext to check if a route / endpoint exists.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top