动态路由 Apache Camel [英] Dynamic routing Apache Camel

查看:62
本文介绍了动态路由 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的现有路由

这篇关于动态路由 Apache Camel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆