camel系列-camel-main

概述

camel-main 提供了一个 Main 扩展类,用于 camel 应用独立运行

加载 Spring DSL

程序入口

  1. 使用 org.apache.camel.spring.Main 类实例化一个对象
  2. 使用 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.
*/
public final class CamelConsoleMain {

private CamelConsoleMain() {
}

public static void main(String[] args) throws Exception {
// Main makes it easier to run a Spring application
Main main = new Main();
// 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();
}
}

spring xml 配置

  1. 在配置文件中声明 camelContext 节点
  2. 在 camelContext 节点中声明 route 节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

<!-- 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>

</beans>

运行效果

这是一个命令行程序,将输入的字母转义成大写字母

1
2
Enter something: hello,world
HELLO,WORLD

Bean 装配

CamelMain 支持 Bean 装配,这一点类似 Spring

  1. 使用addConfigurationClass添加要装配的配置类
  2. 使用BindToRegistry注解声明要装配的类实例
  3. 使用PropertyInject注解声明要注入的 properties 配置字段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public static void main(String[] args) throws Exception {
// use Camels Main class
Main main = new Main();
main.configure().addConfigurationClass(MyConfiguration.class);
}

public class MyConfiguration {

@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)
return new MyBean(hi, bye);
}
}

public class MyBean {

private String hi;
private String bye;

public MyBean(String hi, String bye) {
this.hi = hi;
this.bye = bye;
}

public String hello() {
return hi + " how are you?";
}

public String bye() {
return bye + " World";
}
}

application.properties 清单

1
2
3
# application properties
hi = Hello
bye = Bye

路由资源加载

全局加载

RoutesIncludePattern 类似于全局加载,不需要指定加载目录

camel.main.routesIncludePattern:用于从注册表或通过类路径扫描收集的包容性过滤 RouteBuilder 类。排他过滤优先于包含过滤。该模式使用 Ant-path 样式模式。可以用逗号分隔多个模式。可以用逗号分隔多个模式。例如,要包含以 Foo 开头的所有类,请使用:**/Foo 要包含来自特定包的所有路由,请使用:com/mycompany/foo/* 要包含来自特定包及其子包的所有路由,请使用双通配符:com/mycompany/foo/** 要包含来自两个特定包的所有路由,请使用:com/mycompany/foo/,com/mycompany/stuff/

如下代码清单

1
2
3
4
5
6
public static void main(String[] args) throws Exception {
Main main = new Main();
main.configure().addConfigurationClass(MyConfiguration.class);
main.configure().withRoutesIncludePattern("routes/*.xml");
main.run(args);
}

或者在 application.properties 中配置

1
camel.main.routes-include-pattern = routes/*.xml

路由重载

Camel 中的路由重载功能能够监视目录文件夹中的文件更改,然后自动触发重载 Camel 应用程序中正在运行的路由。

此功能旨在用于开发目的,而不是用于生产用途。

routesReloadEnabled 用于启用自动路由重新加载。如果启用,那么 Camel 将监视给定重新加载目录中的文件更改,并在文件更改时触发重新加载路由。
routesReloadDirectory 用于扫描路由更改的目录。Camel 无法扫描类路径,因此必须将其配置为文件目录。使用 Maven 作为构建工具进行开发,您可以将目录配置为 src/main/resources 以扫描 XML 或 YAML 文件中的 Camel 路由。
routesReloadPattern 用于对来自目录的路由进行包容性过滤。典型用于指定接受 XML 或 YAML 文件中的路由。默认模式为 .yaml,.xml 可以指定多个模式,以逗号分隔。

在应用中指定参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void main(String[] args) throws Exception {
// use Camels Main class
Main main = new Main();
// 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

DSL 配置

1
2
3
4
5
6
7
8
9
<routes id="camel" xmlns="http://camel.apache.org/schema/spring">
<route id="foo3">
<from uri="quartz:foo?cron={{myCron}}"/>
<bean ref="myBean" method="hello"/>
<log message="${body}"/>
<bean ref="myBean" method="bye"/>
<log message="${body}"/>
</route>
</routes>

YAML DSL

application.properties 配置清单

1
2
3
4
5
6
7
8
# 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

DSL 配置

1
2
3
4
5
6
7
8
- route:
id: "foo"
from: "quartz:foo?cron={{myCron}}"
steps:
- to: "bean:myBean?method=hello"
- log: "${body}"
- to: "bean:myBean?method=bye"
- log: "${body}"