在 Java 中在运行时添加骆驼路线 [英] Add camel route at runtime in Java

查看:29
本文介绍了在 Java 中在运行时添加骆驼路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Java 运行时添加骆驼路线?我找到了一个 Grails 示例,但我已经用 Java 实现了它.

How can I add a camel route at run-time in Java? I have found a Grails example but I have implement it in Java.

我的 applicationContext.xml 已经有一些预定义的静态路由,我想在运行时向它添加一些动态路由.是否可以?因为包含动态路由的唯一方法是编写 route.xml,然后将路由定义加载到上下文中.它将如何在现有静态路由上工作?运行时路由

My applicationContext.xml already has some predefined static routes and I want to add some dynamic routes to it at run time. Is it possible? Because the only way to include dynamic route is to write the route.xml and then load the route definition to context. How will it work on existing static routes? Route at runtime

推荐答案

你可以简单地在 CamelContext 上调用几个不同的 API 来添加路由......就像这样

you can simply call a few different APIs on the CamelContext to add routes...something like this

context.addRoutes(new MyDynamcRouteBuilder(context, "direct:foo", "mock:foo"));
....
private static final class MyDynamcRouteBuilder extends RouteBuilder {
    private final String from;
    private final String to;

    private MyDynamcRouteBuilder(CamelContext context, String from, String to) {
        super(context);
        this.from = from;
        this.to = to;
    }

    @Override
    public void configure() throws Exception {
        from(from).to(to);
    }
}

请参阅此单元测试以获取完整示例...

see this unit test for the complete example...

https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/AddRoutesAtRuntimeTest.java

这篇关于在 Java 中在运行时添加骆驼路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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