Camel提供了两种定义路由的形式, html
一种是JAVADSL咱们定义一个RoutBuilder 在config里面定义form("bean:a").to("bean:b") java
还有一种是SPringDSL 经过XML的形式来配置. app
若是咱们须要在应用启动后来动态的增长或者修改路由该怎么办呢. ui
能够经过与beanShell的配合达到上述目的, spa
首先定义好一个RouteDefinition对象rd,放入beanShell的执行器Interpreter中, orm
写好路由定义脚本采用javaDSL : rd.from("bean:a).to("bean:ss") htm
执行脚本,而后camel容器再加载rd便可. 对象
代码以下: blog
1
2
3
4
5
6
7
8
|
String script ="rd.from(\"bean:a\").to(\"bean:b\")";
Interpreter scriptInterpreter =newInterpreter();
scriptInterpreter.set("rd", rd);
scriptInterpreter.eval(script);
scriptInterpreter.eval(script);
rd.setId("RD1001");//给路由设置ID
rd.markPrepared();
camelContext.startRoute(rd);
|
固然在脚本定义路由时不能直接用camel提供的一些表达式,如bean(),simple()等.须要写成rd.from("bean:b").rd.bean("c") ip
若是以为这样不简洁的话,还能够采用自定义Component的形式来解决这个问题.