Best way to load dynamically routes in Apache Camel

眉间皱痕 提交于 2019-12-01 06:15:29

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.

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

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>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!