第 5 章 XML IO DSL
xml-io-dsl 是 Camel 优化的 XML DSL,具有非常快速、低的开销 XML 解析器。它是特定于 Camel 的源代码,只能解析 Camel .xml 路由文件(非经典 Spring < beans> XML 文件)。
我们建议您将 xml-io-dsl 而不是 xml-jaxb-dsl 用于 Camel XML DSL。它适用于所有 Camel 运行时。
当您使用 XML IO DSL 时,camel-spring-boot 应用程序将默认在 src/main/resources/camel8:0:1::.xml 中查找 xml 文件。
您可以通过在 camel.springboot.routes-include-pattern 属性中提供不同的路径来配置此行为:
camel.springboot.routes-include-pattern=/path/to/*.xml
5.1. Example 复制链接链接已复制到粘贴板!
可以使用 Camel CLI 或 Camel K 加载并运行以下 my-route.xml 源文件:
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 声明要绑定到 Camel Registry 的一些 Bean。
您可以在 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 主,而无需实际运行任何 Spring 应用程序上下文(或 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,该 bean 使用 Spring < bean> 元素定义。
Apache Camel JBang 页面中可以找到更多示例。