spring 在 Java 中在运行时添加骆驼路线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10451444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:06:46  来源:igfitidea点击:

Add camel route at runtime in Java

springapache-camel

提问by Himanshu Yadav

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.

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

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

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

回答by Ben ODay

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

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

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

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

回答by Ramkumar S

@Himanshu, Please take a look at dynamicroute options (in other words routing slip) that may help you dynamically route to different 'destinations' based on certain condition.

@Himanshu,请查看动态路由选项(即路由单),它可以帮助您根据特定条件动态路由到不同的“目的地”。

Check the dynamic router help link in camel site;

查看camel站点中的动态路由器帮助链接;

http://camel.apache.org/dynamic-router.html

http://camel.apache.org/dynamic-router.html

from("direct:start")
    // use a bean as the dynamic router
    .dynamicRouter(method(DynamicRouterTest.class, "slip"));

And within the slip method;

和内滑法;

/**
 * Use this method to compute dynamic where we should route next.
 *
 * @param body the message body
 * @return endpoints to go, or <tt>null</tt> to indicate the end
 */
public String slip(String body) {
    bodies.add(body);
    invoked++;

    if (invoked == 1) {
        return "mock:a";
    } else if (invoked == 2) {
        return "mock:b,mock:c";
    } else if (invoked == 3) {
        return "direct:foo";
    } else if (invoked == 4) {
        return "mock:result";
    }

    // no more so return null
    return null;
}

Hope it helps...

希望能帮助到你...

Thanks.

谢谢。