317.2. Camel スキーマの追加

Camel 1.x の場合、次の名前空間を使用する必要があります。

http://activemq.apache.org/camel/schema/spring

スキーマの場所は次のとおりです。

http://activemq.apache.org/camel/schema/spring/camel-spring.xsd

Camel を schemaLocation 宣言に追加する必要があります。

http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd

したがって、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.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

317.2.1. キャメルの使用: 名前空間

または、XML 宣言で camel XSD を参照できます。

xmlns:camel="http://camel.apache.org/schema/spring"
  1. したがって、宣言は次のとおりです。
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
  1. camel: 名前空間接頭辞を使用すると、インライン名前空間宣言を省略できます。
<camel:camelContext id="camel5">
  <camel:package>org.apache.camel.spring.example</camel:package>
</camel:camelContext>

317.2.2. Spring を使用した高度な設定

詳細については、Spring を使用した CamelContext の高度な設定を 参照してください。

$ # Java コードの使用

Java コードを使用して RouteBuilder 実装を定義できます。これらは、Spring で Bean として定義し、camel コンテキストで参照できます。

317.2.3. <package> の使用

Camel は、特定のパッケージ内のルートの自動検出と初期化を可能にする強力な機能も提供します。これは、Spring コンテキスト定義の camel コンテキストにタグを追加し、RouteBuilder 実装を再帰的に検索するパッケージを指定することによって設定されます。1.X でこの機能を使用するには、検索するパッケージのコンマ区切りリストを指定する <package></package> タグが必要です。

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <package>org.apache.camel.spring.config.scan.route</package>
  </camelContext>

警告: パッケージ名を org.apache.camel またはこのサブパッケージとして指定する場合は注意してください。これにより、Camel は独自のパッケージでルートを検索し、問題が発生する可能性があります。

情報:*インスタンス化済みのクラスは無視されます*。<package> と <packageScan> は、Spring などによってすでに作成されているクラスをスキップします。そのため、ルートビルダーを Spring Bean タグとして定義すると、そのクラスはスキップされます。<routeBuilder ref="theBeanId"/> または <contextScan> 機能を使用して、これらの Bean を含めることができます。

317.2.4. <packageScan> の使用

Camel 2.0 ではこれが拡張され、Ant のようなパスマッチングを使用して、検出されたルートクラスを選択的に含めたり除外したりできるようになりました。Spring には、これは <packageScan/> タグを追加することで指定されます。タグには、1 つ以上の package 要素 (1.x と同様) を含める必要があり、オプションで、検出されたクラスの完全修飾名に適用されるパターンを指定する 1 つ以上の includes または excludes 要素を含める必要があります。例えば

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <packageScan>
      <package>org.example.routes</package>
      <excludes>**.*Excluded*</excludes>
      <includes>**.*</includes>
    </packageScan>
  </camelContext>

exclude パターンは、include パターンの前に適用されます。include パターンまたは exclude パターンが定義されていない場合、パッケージで検出されたすべての Route クラスが返されます。

上記の例では、camel はすべての org.example.routes パッケージと RouteBuilder クラスのサブパッケージをスキャンします。スキャンで 2 つの RouteBuilders が見つかったとします。1 つは org.example.routes にある MyRoute、もう 1 つはサブパッケージ excluded にある MyExcludedRoute です。各クラスの完全修飾名が抽出され (org.example.routes.MyRoute、org.example.routes.excluded.MyExcludedRoute)、include および exclude パターンが適用されます。

exclude パターン *.*Excluded は、 fqcn 'org.example.routes.excluded.MyExcludedRoute' に一致し、camel がそれを初期化することを拒否します。

内部では、これは Spring の AntPatternMatcher 実装を使用しており、次のように一致します。

? matches one character
* matches zero or more characters
** matches zero or more segments of a fully qualified name

以下に例を示します。

*.*Excluded は、org.simple.Excluded、org.apache.camel.SomeExcludedRoute、または org.example.RouteWhichIsExcluded に一致します。

*.??included は org.simple.IncludedRoute、org.simple.Excluded と一致しますが、org.simple.PrecludedRoute とは一致しません

317.2.5. contextScan の使用

Camel 2.4 以降で利用可能

Camel がコンテナーコンテキスト (ルートビルダーインスタンスの Spring ApplicationContext など) をスキャンできるようにすることができます。これにより、Spring の <component-scan> 機能を使用して、スキャンプロセスで Spring によって作成された RouteBuilder インスタンスを Camel にピックアップさせることができます。

これにより、Spring @Component を使用してルートにアノテーションを付け、それらのルートを Camel に含めることができます。

@Component
public class MyRoute extends SpringRouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:start").to("mock:result");
    }
}

上記の <packageScan> ドキュメントで説明されているように、inclusion と exclusion に ANT スタイルを使用することもできます。