Apache Camel - 與 Spring 結合使用



現在我們將使用 Spring 重新建立上一章中的應用程式。這將使我們瞭解如何在 XML 中建立 Camel 路由,而不是使用 DSL。

建立新專案

建立一個新的**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");

接下來,我們建立**CamelContext**,並在其引數中指定上面建立的**ApplicationContext**。

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 查詢如下,請注意,我們現在選擇所有“oil”的訂單。

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

輸出端點是多個。第一個端點指定**order**資料夾,第二個端點指定控制檯。

<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。

廣告

© . All rights reserved.