Apache-camel-using-with-spring

提供:Dev Guides
移動先:案内検索

Apache Camel-Springでの使用

Springを使用して、前の章からアプリケーションを再作成します。 これにより、DSLではなくXMLでCamelルーティングを作成する方法がわかります。

新しいプロジェクトの作成

新しい Maven プロジェクトを作成し、次を指定します-

GroupId: BasketWithSpring
ArtifactId: BasketWithSpring

プロジェクトのデフォルトの場所を選択するか、選択したディレクトリを指定します。

依存関係の追加

以前のアプリケーションで使用したコア依存関係に加えて、Springを使用するためにいくつかの依存関係を追加する必要があります。 依存関係はpom.xmlに追加されます。 今、pom.xmlを開き、次の依存関係を追加します-

<dependencies>
   ...
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.3.RELEASE</version>
   </dependency>

   <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-pool</artifactId>
      <version>5.15.2</version>
   </dependency>

   <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-pool</artifactId>
      <version>5.15.1</version>
   </dependency>

   <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>2.15.1</version>
   </dependency>
</dependencies>

Spring用のJava DSLの作成

*DistributeOrderXML* という新しいJavaクラスを作成しましょう。 それに次のコードを追加します-
public class DistributeOrderXML {
   public static void main(String[] args) throws Exception {
      ApplicationContext appContext = new ClassPathXmlApplicationContext(
         "SpringRouteContext.xml");
      CamelContext camelContext = SpringCamelContext.springCamelContext(appContext, false);
      try {
         camelContext.start();
         ProducerTemplate orderProducerTemplate = camelContext.createProducerTemplate();
         InputStream orderInputStream = new FileInputStream(ClassLoader.getSystemClassLoader()
            .getResource("order.xml").getFile());

         orderProducerTemplate.sendBody("direct:DistributeOrderXML", orderInputStream);
      } finally {
         camelContext.stop();
      }
   }
}
*main* メソッドでは、まず、 *ApplicationContext* のインスタンスを作成します。これは、Springアプリケーション内の中心的なインターフェイスです。 コンストラクターで、ルーティングおよびフィルター情報を含むXMLファイルの名前を指定します。
ApplicationContext appContext = new ClassPathXmlApplicationContext(
   "SpringRouteContext.xml");

次に、上記で作成した ApplicationContext をパラメーターに指定して CamelContext を作成します。

CamelContext camelContext = SpringCamelContext.springCamelContext(appContext, false);

この時点で、ルーティングとフィルタリングが設定されました。 したがって、 start メソッドを使用して CamelContext を開始します。 前の場合と同様に、order.xmlファイルをロードするためのエンドポイントを定義し、処理を開始します。 ここで、XMLでルーティングがどのように定義されるかを理解しましょう。

アプリケーションコンテキストの作成

プロジェクトに新しいXMLファイルを追加し、それを* SpringRouteContext.xml。*と呼びます。次の内容をこのファイルにカットアンドペーストします。

<?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.xsd
      http://camel.apache.org/schema/spring
      http://camel.apache.org/schema/spring/camel-spring.xsd ">
   <camelContext xmlns = "http://camel.apache.org/schema/spring">
      <route>
         <from uri = "direct:DistributeOrderXML"/>
         <log message = "Split by Distribute Order"/>
         <split>
            <xpath>//order[@product = 'Oil']/items</xpath>
            <to uri = "file:src/main/resources/order/"/>
            <to uri = "stream:out"/>
         </split>
      </route>
   </camelContext>
</beans>

ここで、xpathクエリを次のように定義します。「オイル」のすべての注文を選択することに注意してください。

<xpath>//order[@product = 'Oil']/items</xpath>

出力エンドポイントは複数です。 最初のエンドポイントは order フォルダーを指定し、2番目のエンドポイントはコンソールを指定します。

<to uri = "file:src/main/resources/order/"/>
<to uri = "stream:out"/>

アプリケーションを実行してください。

試験結果

アプリケーションを実行すると、画面に次の出力が表示されます。

<items>
   <item>
      <Brand>Cinthol</Brand>
      <Type>Original</Type>
      <Quantity>4</Quantity>
      <Price>25</Price>
   </item>
   <item>
      <Brand>Cinthol</Brand>
      <Type>Lime</Type>
      <Quantity>6</Quantity>
      <Price>30</Price>
   </item>
</items>

指定したパスの order フォルダーをチェックアウトします。 上記のXMLコードを含む新しく作成されたファイルが見つかります。

結論

Camelは、EIPを実装してすぐに使用できるフレームワークを提供し、統合プロジェクトを容易にします。 ドメイン固有の言語でのコーディングとXMLの使用もサポートしています。