动态路由Apache Camel

问题描述:

是否有一些解决方案可以在时间执行中动态创建骆驼路线?通常,我们将骆驼路线明确定义为:

Is there some solution to create a camel route dynamically, in time execution? In a common way, we explicitly define a camel route as:

from("direct:a")           
            .to("direct:b");

但是,我想在需要时在执行时创建一些路由.例如,应用程序将从属性文件中读取属性,并使用属性创建路由.我将得到代码:

However, I want to create some routes in time execution when be required. For example, from a property file, the application will read the properties and create the routes using the properties. I'll have the code:

from({property1}) 
            .to({property2});

如果还存在一个属性文件,则应用程序必须动态创建另一条路由并将其添加到骆驼上下文中.那有可能有人可以帮助我吗?

If exists one more property file the application must create dynamically another route and add it in the camel context. Is that possible, someone can help me?

要在运行时动态创建骆驼路线,您需要

To create camel route(s) dynamically at runtime, you need to

一一使用配置文件

Consume config files 1 by 1

这可以像设置文件端点来使用文件一样简单,例如

This can be as simple as setting a file endpoint to consume files, e.g.

<from uri="file:path/to/config/files?noop=true"/>
    <bean ref="pleaseDefineThisSpringBean" method="buildRoute" />
    ...

通过配置文件创建路由

Create route by config file

在routeBuilder的帮助下向CamelContext添加新路由

Add a new route to CamelContext with the help of a routeBuilder

public void buildRoute(Exchange exchange) {
    // 1. read config file and insert into variables for RouteBuilder
    // 2. create route
    SpringCamelContext ctx = (SpringCamelContext)exchange.getContext();
    ctx.addRoutes(createRoutebyRouteBuilder(routeId, from_uri, to_uri));
}

protected static RouteBuilder createRoutebyRouteBuilder(final String routeId, final String from_uri, String to_uri) throws Exception{

    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {

            from (from_uri)
                .routeId(routeId)
                .to(to_uri);
        }
    };
}

注意:函数"addRoutes"将覆盖具有相同routeId的现有路由