使用 setApplicationContextUri 方法指定待加载的 Spring XML 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import org.apache.camel.spring.Main;
/** * A main class to run the example from your editor. */ publicfinalclassCamelConsoleMain {
privateCamelConsoleMain() { }
publicstaticvoidmain(String[] args)throws Exception { // Main makes it easier to run a Spring application Mainmain=newMain(); // configure the location of the Spring XML file main.setApplicationContextUri("META-INF/spring/camel-context.xml"); // run and block until Camel is stopped (or JVM terminated) main.run(); } }
<!-- camelContext is the Camel runtime, where we can host Camel routes --> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <!-- read input from the console using the stream component --> <from uri="stream:in?promptMessage=Enter something: "/> <!-- transform the input to upper case using the simple language --> <!-- you can also use other languages such as groovy, ognl, mvel, javascript etc. --> <transform> <simple>${body.toUpperCase()}</simple> </transform> <!-- and then print to the console --> <to uri="stream:out"/> </route> </camelContext>
publicstaticvoidmain(String[] args)throws Exception { // use Camels Main class Mainmain=newMain(); main.configure().addConfigurationClass(MyConfiguration.class); }
publicclassMyConfiguration {
@BindToRegistry public MyBean myBean(@PropertyInject("hi") String hi, @PropertyInject("bye") String bye) { // this will create an instance of this bean with the name of the method (eg myBean) returnnewMyBean(hi, bye); } }
用于对来自目录的路由进行包容性过滤。典型用于指定接受 XML 或 YAML 文件中的路由。默认模式为 .yaml,.xml 可以指定多个模式,以逗号分隔。
在应用中指定参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicstaticvoidmain(String[] args)throws Exception { // use Camels Main class Mainmain=newMain(); // lets use a configuration class (you can specify multiple classes) // (properties are automatic loaded from application.properties) main.configure().addConfigurationClass(MyConfiguration.class); // and add all the XML routes main.configure().withRoutesIncludePattern("routes/*.xml"); // turn on reloading routes on code-changes main.configure().withRoutesReloadEnabled(true); main.configure().withRoutesReloadDirectory("src/main/resources"); main.configure().withRoutesReloadPattern("routes/*.xml");
// now keep the application running until the JVM is terminated (ctrl + c or sigterm) main.run(args); }
application.properties 配置清单
1 2 3 4 5 6
# turn on route reloading on file changes camel.main.routes-reload-enabled = true # the base directory to watch camel.main.routes-reload-directory = src/main/resources # pattern(s) for files to watch camel.main.routes-reload-pattern = routes/*.xml
XML && YAML DSL 加载
XML DSL
application.properties 配置清单
1 2 3 4 5 6 7 8 9
# load XML routes camel.main.routes-include-pattern = routes/*.xml # turn on route reloading on file changes camel.main.routes-reload-enabled = true # the base directory to watch camel.main.routes-reload-directory = src/main/resources/ # pattern(s) for files to watch camel.main.routes-reload-pattern = routes/*.xml
# turn on route reloading on file changes camel.main.routes-reload-enabled = true # the base directory to watch camel.main.routes-reload-directory = src/main/resources # pattern(s) for files to watch camel.main.routes-reload-pattern = routes/*.yaml # on reload should all existing routes be removed first camel.main.routes-reload-remove-all-routes = true