我们开发了基于Karaf和Apache Camel的应用程序。虽然我们的应用程序完全基于bundle(OSGI),但我们也在启动时加载Camel上下文(及其“Route Contexts”),这意味着我们已经定义了一些静态路由。
我的问题是。有没有办法在应用程序运行时动态加载路由,而无需重新读取Camel Context,因为这将重置/重新启动已存在的路由。这同样适用于已创建的路线,例如,如果我们要编辑已存在的路线。
整个想法是我们计划将路径放在数据库中,因此,数据库将由GUI编辑。
那么这样做的最佳方法是什么?我不相信,在路由被添加,编辑等情况下,这是重新加载OSGI捆绑/捆绑的最佳方法......
在应用程序运行期间或多或少,将添加,编辑,删除不同的端点及其相关路由。
请指教。
谢谢, Tiho
答案 0 :(得分:6)
我认为一个好的方法是将您的路线分组到每个上下文只有几条(甚至可能是单条)路线的小环境中。然后重新加载该小上下文而不会导致其他路径中断。
但是,由于您不相信这种方法,您可以使用CamelContext上的方法轻松添加和删除路径。创建一个路径构建器,用于构建数据库中的路径并使用addRoutes和removeRoute。
答案 1 :(得分:2)
另请参阅此cookbook示例如何在运行时以xml格式加载/编辑路径 http://camel.apache.org/loading-routes-from-xml-files.html
答案 2 :(得分:0)
动态添加/删除路由不会重启/重置camelContext。
请找到样品。
public class DynamicAddRouteProcessor implements Processor {
@Override
public void process(Exchange paramExchange) throws Exception {
final String routeId = "DYNANMIC.ROUTE.1";
Route route = paramExchange.getContext().getRoute(routeId);
if (null == route) {
System.out.println("No route exist, creating one with name ");
paramExchange.getContext().addRoutes(new RouteBuilder() {
public void configure() throws Exception {
from("direct:DYNANMIC.ROUTE.1").routeId(routeId).to("direct:myloggerRoute");
}
});
} else {
System.out.println("Route already exist, no action"+ route.getId());
}
}
}
公共类DynamicRemoveRouteProcessor实现Processor {
@Override
public void process(Exchange paramExchange) throws Exception {
final String routeId = "DYNANMIC.ROUTE.1";
Route route = paramExchange.getContext().getRoute(routeId);
if (null != route) {
System.out.println("Route already exist, deleting it!!!" + route.getId());
paramExchange.getContext().stopRoute(routeId);
paramExchange.getContext().removeRoute(routeId);
} else {
System.out.println("No sucn route exist, no action done "
+ routeId);
}
}
}
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="timerToLog">
<from uri="timer:foo?period=5000" />
<setBody>
<method ref="helloBean" method="hello" />
</setBody>
<log message="The message contains ${body}" />
<to uri="mock:result" />
</route>
<route id="routeAddition">
<from uri="timer:foo?period=10000" />
<process ref="dynamicAddRouteProcessor" />
<log message="Added new route to context....DONE " />
<delay ><simple>5000</simple></delay>
<process ref="dynamicRemoveRouteProcessor" />
<to uri="mock:result" />
</route>
<route id="myloggerRoute">
<from uri="direct:myloggerRoute" />
<log message="Route add/removal completed - ${body}" />
<to uri="mock:result" />
</route>
</camelContext>