14

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.

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

Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291

3 Answers3

26

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

Ben ODay
  • 20,784
  • 9
  • 45
  • 68
  • Thanks for your response but I am looking for more configurable way of doing it. In my case an external application can decide the routes of my application, I am looking for more configurable solution to this problem – Himanshu Yadav May 04 '12 at 17:35
  • 1
    The solution above look quite configurable to me. Do you want the complete route to be dynamic? Then you can only use xml. – Christian Schneider May 05 '12 at 06:33
  • Its all Java code, so inside the configure method you can use if .. else, call other methods etc. So you can make the DSL very dynamic. Also you can have X number of template classes to use etc. And if you dont like the DSL in the RouteBuilder, you can build routes using the classes in the model package directly. – Claus Ibsen May 05 '12 at 06:50
  • 1
    I've even made a javascript user interface to design routes graphically by EIP building blocks that essentially dynamically built DSL routes in java. Is is also possible to manage the routes dynamically rather easy, deploy, start, stop, remove etc. The Java API in Camel is quite flexible. – Petter Nordlander May 05 '12 at 09:51
  • 2
    @Petter Sounds awesome, got a link? :) – AlanFoster Jul 08 '13 at 18:19
  • @AlanFoster, unfortunatley not Project pretty much died just before we decided what to do with it. :) – Petter Nordlander Jul 09 '13 at 05:36
  • 2
    @Petter, sounds awesome for me as well. Any chance to make it open source? – rwitzel Dec 13 '13 at 10:43
1

@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.

Check the dynamic router help link in camel site;

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.

Ramkumar S
  • 97
  • 5
0

One such solution could be:

Define route:

private RouteDefinition buildRouteDefinition() {
    RouteDefinition routeDefinition = new RouteDefinition();
    routeDefinition.from(XX).to(ZZ); // define any route you want
    return routeDefinition;
}

Get Model Context and create route:

CamelContext context = getContext();
ModelCamelContext modelContext = context.adapt(ModelCamelContext.class);
modelContext.addRouteDefinition(routeDefinition);

There are more way of getting camel context. To name few:

  • In processor, you can use exchange.getContext()
  • Through RouteBuilder reference, you can use routeBuilder.getContext()
Josef Veselý
  • 102
  • 1
  • 4