第 8 章 XML IO DSL
xml-io-dsl 是 Camel optimized XML DSL,其使用非常快且较低的开销 XML 解析器。它是一个由 Camel 特定且只能解析 Camel .xml 路由文件(非经典的 Spring < beans> XML 文件)生成的源代码。
我们建议您在 Camel XML DSL 中使用 xml-io-dsl 而不是 xml-jaxb-dsl。它适用于所有 Camel 运行时。
当您使用 XML IO DSL 时,camel-spring-boot 应用程序将默认在 src/main/resources/camel done.xml 中查找 xml 文件。
您可以通过在 camel.springboot.routes-include-pattern 属性中提供不同的路径来配置此行为:
camel.springboot.routes-include-pattern=/path/to/*.xml
8.1. Example 复制链接链接已复制到粘贴板!
以下 my-route.xml 源文件可以使用 Camel CLI 或 Camel K 加载并运行:
my-route.xml
<routes xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="timer:tick"/>
<setBody>
<constant>Hello Camel K!</constant>
</setBody>
<to uri="log:info"/>
</route>
</routes>
您可以省略 xmlns 命名空间。
如果只有一个路由,您可以使用 <route> 作为 root XML 标签,而不是 < routes>。
使用 Camel K 运行
kamel run my-route.xml
使用 Camel CLI 运行
camel run my-route.xml
您可以使用 xml-io-dsl 声明某些 Bean 以绑定到 Camel Registry。
您可以声明和 Beans 在 XML 中定义其属性(包括 嵌套属性 )。例如:
Bean 声明和定义
<camel>
<bean name="beanFromProps" type="com.acme.MyBean">
<properties>
<property key="field1" value="f1_p" />
<property key="field2" value="f2_p" />
<property key="nested.field1" value="nf1_p" />
<property key="nested.field2" value="nf2_p" />
</properties>
</bean>
</camel>
虽然保持 xml-io-dsl 使用的快速 XML 解析器的所有优势,但 Camel 也可以处理在其他 XML 命名空间中声明的 XML 元素,并单独处理它们。使用这个机制,可以使用 Spring 的 http://www.springframework.org/schema/beans 命名空间包含 XML 元素。
这将 Spring Beans 的灵活性引入 Camel main,而无需实际运行任何 Spring Application Context (或 Spring Boot)。
找到 Spring 命名空间中的元素时,它们用于填充并配置 org.springframework.beans.factory.support.DefaultListableBeanFactory 实例,并利用 Spring 依赖项注入将 Bean 连线在一起。
然后,这些 Bean 通过正常的 Camel Registry 公开,可以被 Camel 路由使用。
以下是一个 camel.xml 文件示例,它定义路由定义使用的路由和 Bean (引用):
camel.xml
<camel>
<beans xmlns="http://www.springframework.org/schema/beans">
<bean id="messageString" class="java.lang.String">
<constructor-arg index="0" value="Hello"/>
</bean>
<bean id="greeter" class="org.apache.camel.main.app.Greeter">
<description>Spring Bean</description>
<property name="message">
<bean class="org.apache.camel.main.app.GreeterMessage">
<property name="msg" ref="messageString"/>
</bean>
</property>
</bean>
</beans>
<route id="my-route">
<from uri="direct:start"/>
<bean ref="greeter"/>
<to uri="mock:finish"/>
</route>
</camel>
my-route 路由引用 greeter bean,它通过 Spring < bean> 元素定义。
可以在 Apache Camel JBang 页面上找到更多示例。