Best way to load dynamically routes in Apache Camel
Asked Answered
L

3

6

we have developped application based on Karaf and Apache Camel. While our application is entirely based on bundles ( OSGI ) we are also loading the Camel context ( and its' Route Contexts ) on startup, whcih would mean that we have defined some static routes.

My question is. Is there a way to dynamically LOAD routes while the application is running without the need to reread the Camel Context as this will reset/restart the already existing routes. The same would apply to already created routes, for example if we want to edit a route whcih already exist.

The whole idea is that we are planning to put the routes in a database and as such, the database will be edited by a GUI.

So what's the best approach to do this? I do not beleive that it is really the best approach to reload OSGI bundle/bundles in case of a route being added, editted etc...

More or less during operation of the application, Different endpoints + their related routes will be added, edited, removed.

Please advise.

Thanks, Tiho

Lornalorne answered 29/4, 2014 at 4:20 Comment(3)
If you are brave, you can dynamically edit routes with hawt.io...Privet
Yeah, we were indeed thinking about it... but that's not a good approach. Rather our intention is to develop our Hawt.io plugin to do this for us if we tend to use database for the routes.Lornalorne
Conceptually, I think the routes should be largely static and not need to be changed at runtime. Logical choices within the routes and things like dynamic routers should usually be enough. If not, I would take a step back and think about whether they could be designed differently.Privet
A
6

I think a good approach is to group your routes into small contexts with just a few (or maybe even single) routes per context. Then you reload that small context without causing interruption in other routes.

However, as you don't believe in that approach, you can easily add and remove routes with methods on the CamelContext. Create a route builder that constructs routes from your database and use addRoutes and removeRoute.

Astri answered 29/4, 2014 at 5:52 Comment(0)
A
3

See also this cookbook example how to load/edit routes form xml format at runtime http://camel.apache.org/loading-routes-from-xml-files.html

Alkaloid answered 29/4, 2014 at 6:11 Comment(1)
Link is broken, could you update your answer?Sorority
T
2

Adding/Removing routes dynamically does not restart/reset camelContext.

Please find the sample.

DynamicAddRouteProcessor.java

public class DynamicAddRouteProcessor implements Processor {
    @Override
    public void process(Exchange paramExchange) throws Exception {

        final String routeId = "DYNANMIC.ROUTE.1";
        Route route = paramExchange.getContext().getRoute(routeId);
        if (null == route) {
            System.out.println("No route exist, creating one with name "); 
            paramExchange.getContext().addRoutes(new RouteBuilder() {
                public void configure() throws Exception {
                    from("direct:DYNANMIC.ROUTE.1").routeId(routeId).to("direct:myloggerRoute");
                }
            });
        } else {
            System.out.println("Route already exist, no action"+ route.getId());
        }
    }
}

DynamicRemoveRouteProcessor.java

public class DynamicRemoveRouteProcessor implements Processor {

    @Override
    public void process(Exchange paramExchange) throws Exception {

        final String routeId = "DYNANMIC.ROUTE.1";
        Route route = paramExchange.getContext().getRoute(routeId);
        if (null != route) {
            System.out.println("Route already exist, deleting it!!!"    + route.getId());
            paramExchange.getContext().stopRoute(routeId);
            paramExchange.getContext().removeRoute(routeId);
            
        } else {
            System.out.println("No sucn route exist, no action done "
                    + routeId);
        }
    }
}

blueprint.xml

<camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <route id="timerToLog">
        <from uri="timer:foo?period=5000" />
        <setBody>
            <method ref="helloBean" method="hello" />
        </setBody>
        <log message="The message contains ${body}" />
        <to uri="mock:result" />
    </route>
    <route id="routeAddition">
        <from uri="timer:foo?period=10000" />
        <process ref="dynamicAddRouteProcessor" />
        <log message="Added new route to context....DONE " />
        <delay ><simple>5000</simple></delay>
        <process ref="dynamicRemoveRouteProcessor" />
        <to uri="mock:result" />
    </route>

    <route id="myloggerRoute">
        <from uri="direct:myloggerRoute" />
        <log message="Route add/removal completed - ${body}" />
        <to uri="mock:result" />
    </route>
</camelContext>
Tibiotarsus answered 17/8, 2017 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.