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.
...
Create route by config file
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);
}
};
}
Note: The function "addRoutes" will override existing route of same routeId