ServiceMix Kernel 上で Camel を使用

前回(id:fits:20080929)は ServiceMix 3.2.2 上で Camel を実行してみたが、今回は ServiceMix 4.0 のカーネルを担う ServiceMix Kernel 1.0.0 を使って Camel を実行してみる。(ServiceMix Kernel は OSGi ベースの軽量コンテナ)

Camel 実行に必要なパッケージのインストール

ServiceMix Kernel はバイナリ用のアーカイブファイルをダウンロードして適当なディレクトリに解凍するだけで実行できるようになるが、この状態ではまだ Apache Camel は使えない。

Camel を使えるようにするには、以下のパッケージを ServiceMix Kernel の管理コンソールからインストールしてやる必要がある。

  • spring-tx
  • camel-core
  • camel-spring

インストールは bin/servicemix.bat(windowsの場合)で起動する管理コンソール上で、以下のように install -s xxxx を繰り返す事で実施する。
ちなみに -s オプション(--start の短縮)はインストール後に開始(start)するための指定。

>servicemix.bat
・・・
servicemix>osgi install -s mvn:org.springframework/spring-tx/2.5.5
servicemix>osgi install -s mvn:org.apache.camel/camel-core/1.4.0
servicemix>osgi install -s mvn:org.apache.camel/camel-spring/1.4.0

なお、管理コンソール上では以下のようなコマンドを使ってパッケージを管理する事が可能となっている。

  • osgi list パッケージのリストアップ
  • osgi start [id] パッケージの有効化
  • osgi stop [id] パッケージの無効化
  • osgi uninstall [id] パッケージのアンインストール

また、管理コンソールの操作ログはユーザーディレクトリ(windowsでは %USERPROFILE%)の .servicemix/servicemix.history ファイルに残される。

プロキシサーバーを使っている場合

プロキシサーバーを使ってインターネットに接続しているような環境では以下のようなエラーが発生して osgi install が失敗する。

servicemix> osgi install -s mvn:org.apache.camel/camel-core/1.4.0
java.lang.RuntimeException: URL [mvn:org.apache.camel/camel-core/1.4.0] 
could not be resolved. (enable TRACE logging for details)
ERROR NullPointerException: null

このような場合は、環境変数 JAVA_OPTS にプロキシ用のシステムプロパティ設定を行ってから servicemix.bat を起動するようにすればよい。

>set JAVA_OPTS=-Dhttp.proxyHost=プロキシサーバーのホスト名
>servicemix.bat

Camel XML Configuration ファイルのデプロイ

Apache Camel で実行可能な XML Configuration ファイルを ServiceMix Kernel の deploy ディレクトリに配置すればデプロイが完了する。

test-camel.xml ファイル
<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://activemq.apache.org/camel/schema/spring 
         http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

    <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
        <route>
            <from uri="file:test_camel" />
            <to uri="log:test" />
        </route>
    </camelContext>
</beans>

上記ファイルを deploy ディレクトリに配置し、osgi list コマンドを実行すると test-camel.xml が追加されている事を確認できる。

servicemix>osgi list
・・・
[  37] [Active     ] [   50] test-camel.xml (0.0.0)

動作確認

カレントディレクトリに生成された test_camel ディレクトリにファイルを配置すると .camel ディレクトリにファイルが移され、ファイルの内容が ServiceMix Kernel のインストールディレクトリ内の data/log/servicemix.log ファイルに出力される。

ちなみに、log d コマンド(log display の短縮)を実行すれば servicemix.log ファイルの内容が標準出力に出力される。

servicemix>log d

[備考] Camel 上で XML Configuration ファイルを実行する方法

上記で使用した test-camel.xml ファイルを Apache Camel 上で実行するには、適切なクラスパスが設定されている状態で、以下のように org.apache.camel.spring.Main クラスの実行時引数として指定してやればよい。

>java org.apache.camel.spring.Main -fa test-camel.xml