38.3.2. コンポーネントの設定
概要
コンポーネントを追加する場合は、Apache Camel Spring 設定ファイル (META-INF/spring/camel-context.xml) でコンポーネントを設定します。コンポーネントを見つけるために、コンポーネントの URI プレフィックスは Spring 設定の bean 要素の ID 属性と照合されます。コンポーネントプレフィックスが bean 要素 ID と一致する場合、Apache Camel は参照されたクラスをインスタンス化し、Spring 設定に指定されたプロパティーを注入します。
このメカニズムは自動検出よりも優先されます。CamelContext が必須 ID で Spring Bean を見つけた場合は、自動検出を使用したコンポーネントの検索は行われません。
コンポーネントクラスで Bean プロパティーを定義します。
コンポーネントクラスに注入したいプロパティーがある場合は、これを Bean プロパティーとして定義します。以下に例を示します。
public class CustomComponent extends DefaultComponent<CustomExchange> { ... PropType getProperty() { ... } void setProperty(PropType v) { ... } }
getProperty() メソッドと setProperty() メソッドは、プロパティー の値にアクセスします。
Spring のコンポーネントの設定
Spring でコンポーネントを設定するには、例38.1「Spring でのコンポーネントの設定」 にあるように設定ファイル META-INF/spring/camel-context.xml を編集します。
例38.1 Spring でのコンポーネントの設定
<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<package>RouteBuilderPackage</package>
</camelContext>
<bean id="component-prefix" class="component-class-name">
<property name="property" value="propertyValue"/>
</bean>
</beans>
component-prefix の ID が付いた bean 要素は component-class-name コンポーネントを設定します。property 要素を使用して、プロパティーをコンポーネントインスタンスに注入することができます。たとえば、前述の例の property 要素は、コンポーネントで setProperty() を呼び出して、propertyValue の値を property プロパティーに注入します。
例
例38.2「Spring JMS コンポーネントの設定」 は、ID が jms と等しい bean 要素を定義して Apache Camel の JMS コンポーネントを設定する方法の例を示しています。これらの設定は Spring 設定ファイル (camel-context.xml) に追加されます。
例38.2 Spring JMS コンポーネントの設定
<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<package>org.apache.camel.example.spring</package> 1
</camelContext>
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> 2
<property name="connectionFactory"3
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL"
value="vm://localhost?broker.persistent=false&broker.useJmx=false"/> 4
</bean>
</property>
</bean>
</beans>- 1
CamelContextは、指定された Java パッケージ (org.apache.camel.example.spring) にあるRouteBuilderクラスを自動的にインスタンス化します。- 2
- ID
jmsの付いた bean 要素は、JMS コンポーネントを設定します。Bean ID は、コンポーネントの URI プレフィックスに対応します。たとえば、ルートが URI でエンドポイント (jms://MyQName) を指定する場合、Apache Camel はjmsbean 要素の設定を使用して JMS コンポーネントを自動的に読み込みます。 - 3
- JMS は、メッセージングサービスのラッパーです。
JmsComponentクラスにconnectionFactoryプロパティーを設定して、メッセージングシステムの具象実装を指定する必要があります。 - 4
- この例では、JMS メッセージングサービスの具象実装は Apache ActiveMQ です。
brokerURLプロパティーは、メッセージブローカーがローカルの Java 仮想マシン (JVM) に組み込まれている ActiveMQ ブローカーインスタンスへの接続を初期化します。ブローカーが JVM にない場合、ActiveMQ はオプションbroker.persistent=false(ブローカーはメッセージを永続化しない) およびbroker.useJmx=false(ブローカーは JMX ポートを開かない) でインスタンス化します。