第6章 XML IO DSL
xml-io-dsl
は、非常に高速でオーバーヘッドの少ない XML パーサーを備えた、Camel に最適化された XML DSL です。これは Camel 固有のソースコード生成パーサーであり、Camel .xml
ルートファイル (従来の Spring <beans>
XML ファイルではない) のみを解析できます。
Camel XML DSL には、xml-jaxb-dsl
の代わりに xml-io-dsl
を使用することを推奨します。すべての Camel ランタイムで動作します。
XML IO DSL を使用している場合、camel-spring-boot
アプリケーションはデフォルトで src/main/resources/camel/*.xml
ファイルを検索します。
この動作を設定するには、camel.springboot.routes-include-pattern
プロパティーに別のパスを指定します。
camel.springboot.routes-include-pattern=/path/to/*.xml
6.1. 例
以下の 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
namespace は省略できます。
ルートが 1 つしかない場合は、<routes>
の代わりに <route>
をルート XML タグとして使用できます。
Camel K での実行
kamel run my-route.xml
Camel CLI を使用した実行
camel run my-route.xml
xml-io-dsl
を使用して、Camel Registry にバインドされる一部の Bean を宣言できます。
Bean は XML でプロパティー (nested
プロパティーを含む) を定義でき、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 namespace で宣言された XML 要素を処理し、それらを個別に処理することもできます。このメカニズムでは、Spring の http://www.springframework.org/schema/beans
namespace を使用して XML 要素を含めることができます。
これにより、Spring Application Context (または Spring Boot) を実際に実行しなくても、Spring Beans の柔軟性が Camel メインにもたらされます。
Spring namespace の要素が見つかると、それらは org.springframework.beans.factory.support.DefaultListableBeanFactory
のインスタンスの入力および設定に使用され、Spring 依存関係インジェクションを利用して Bean を接続します。
その後、これらの Bean は通常の Camel Registry を介して公開され、Camel ルートで使用できます。
以下は、ルート定義によって使用されるルートと Bean の両方を定義する camel.xml
ファイルの例です。
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
ルートは、Spring <bean>
要素を使用して定義された greeter
を参照します。
その他の例は Apache Camel JBang ページにあります。