300.9.5.3. 使用 XML 文件

您还可以使用 XML DSL 定义 Camel 路由,尽管我们不使用 Spring 或 Blueprint,而 XML 文件只能包含 Camel 路由。

在 web.xml 中,您可以引用来自 "classpath"、"file" 或 "http" url 的 XML 文件,如下所示:

  <context-param>
    <param-name>routeBuilder-MyRoute</param-name>
    <param-value>classpath:routes/myRoutes.xml</param-value>
  </context-param>

XML 文件是:

routes/myRoutes.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- the xmlns="http://camel.apache.org/schema/spring" is needed -->
<routes xmlns="http://camel.apache.org/schema/spring">

  <route id="foo">
    <from uri="direct:foo"/>
    <to uri="mock:foo"/>
  </route>

  <route id="bar">
    <from uri="direct:bar"/>
    <to uri="mock:bar"/>
  </route>

</routes>

请注意,在 XML 文件中,root 标签为 <routes>,必须使用命名空间 "http://camel.apache.org/schema/spring"。此命名空间在名称中包含 spring,但由于历史原因,因为 Spring 是首次存在,因此此时只有 XML DSL。在运行时不需要 Spring JAR。或许在 Camel 3.0 中,命名空间可以重命名为通用名称。