ServiceMix 上で Camel を使用

オープンソース ESB の Apache ServiceMix では、Apache Camel を使用するための servicemix-camel コンポーネントが用意されている。
そこで今回は、ServiceMix 3.2.2 上で Camel のルーティングルールを実行してみる事にする。

servicemix.xml の作成

ServiceMix のコンテナ構成ファイル(servicemix.xml)に Apache Camel のルーティングルールを定義する。基本的に Apache Camel の Spring XML を使った構成ファイル(camel-context.xml)に ServiceMix 用のコンテナ定義を追加するだけでよい。

  • org.apache.servicemix.camel.CamelJbiComponent コンポーネントをコンテナ内で定義
  • camelContext 要素に Camel のルールを記述(Apache Camel の Spring XML を使った構成ファイルと同様)

今回は、以下のようなルール定義のファイル servicemix-camel.xml を作成した。

  1. test ディレクトリを監視
  2. test ディレクトリにファイルが配置されると標準出力にファイル内容を出力
servicemix-camel.xml ファイル
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:sm="http://servicemix.apache.org/config/1.0"> 

    <bean id="jndi" class="org.apache.xbean.spring.jndi.SpringInitialContextFactory" factory-method="makeInitialContext" singleton="true" />

    <sm:container id="jbi" useMBeanServer="true" createMBeanServer="true">
        <sm:activationSpecs>
            <sm:activationSpec>
                <sm:component>
                    <bean class="org.apache.servicemix.camel.CamelJbiComponent" />
                </sm:component>
            </sm:activationSpec>
        </sm:activationSpecs>
    </sm:container>

    <!-- Camel の設定 -->
    <camelContext useJmx="true" xmlns="http://activemq.apache.org/camel/schema/spring">
        <!-- ルーティングルールの設定 -->
        <route>
            <from uri="file:test" />
            <to uri="log:test" />
        </route>
    </camelContext>
</beans>

実行

ここで、以下のように ServiceMix インストールディレクトリの bin/servicemix.bat を使って servicemix-camel.xml ファイルを実行すると、java.lang.ClassNotFoundException が発生して ServiceMix が起動しない。

>servicemix.bat servicemix-camel.xml
・・・
Bean ''; nested exception is java.lang.ClassNotFoundException: org.apache.servicemix.camel.CamelJbiComponent
Caused by: java.lang.ClassNotFoundException: org.apache.servicemix.camel.CamelJbiComponent
・・・

これは以下のコンポーネントがロードされていないため発生すると考えられる。

  • servicemix-camel-3.2.2-installer.zip
  • servicemix-shared-3.2.2-installer.zip

そのため、今回はこれを回避するために、上記 zip ファイルに含まれる全ての jar ファイルを ServiceMix インストールディレクトリの conf/servicemix.conf を編集し、load 対象として追加したディレクトリに配置する事にした。(他に良い方法があると思う)

具体的には、conf/servicemix.conf に以下のような設定を加え、カレントディレクトリ内の lib ディレクトリに上記 zip ファイル内の jar ファイルを配置した。

conf/servicemix.conf の編集
main is org.apache.servicemix.Main from app

[app]
    load ${servicemix.home}/conf
    load ${servicemix.home}/lib/*.jar
    load ${servicemix.home}/lib/optional/*.jar

    #下記の設定を追加(カレントディレクトリ lib 内の全 jar ファイルをロード)
    load ./lib/*.jar

上記の設定により、以下のコマンド実行で ServiceMix が起動するようになり、test ディレクトリにファイルと配置すると標準出力にファイル内容が出力される事が確認できた。

>servicemix.bat servicemix-camel.xml
INFO  - JBIContainer                   - ServiceMix 3.2.2 JBI Container (ServiceMix) is starting
・・・
INFO  - JBIContainer                   - ServiceMix JBI Container (ServiceMix) started