- Apache Camel 教程
- Apache Camel - 首頁
- Apache Camel - 簡介
- Apache Camel - 概述
- Apache Camel - 功能特性
- Apache Camel - 架構
- Apache Camel - CamelContext
- Apache Camel - 端點
- Apache Camel - 元件
- Apache Camel - 訊息佇列
- Apache Camel 專案
- 使用 Camel 與 Spring 整合
- Apache Camel 有用資源
- Apache Camel - 快速指南
- Apache Camel - 有用資源
- Apache Camel - 討論
Apache Camel 專案
我們將使用 Maven 來構建 Camel 專案。不過,我們更建議使用 IntelliJ IDE 進行開發。您可以選擇任何您喜歡的 IDE 來完成此專案。
建立新專案
建立一個新的Maven專案並指定以下內容:
GroupId: Basket ArtifactId: Basket
選擇專案預設位置,或者如果您願意,可以指定您選擇的目錄。
新增依賴項
您需要新增一些依賴項才能使用 Camel。這些依賴項新增到pom.xml檔案中。因此,開啟 pom.xml 並新增以下兩個依賴項:
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>2.20.0</version>
</dependency>
</dependencies>
注意 - 我們只需要應用程式的最低限度依賴項。當您從其庫中使用更多 Camel 元件時,您需要在此 pom.xml 檔案中新增相應的依賴項。
建立 Java DSL
接下來,您將在 Java DSL 中編寫過濾和路由程式碼。建立一個名為DistributeOrderDSL的新 Java 類。向其中新增以下程式碼:
public class DistributeOrderDSL {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
try {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:DistributeOrderDSL")
.split(xpath("//order[@product='soaps']/items")).to("stream:out");
// .to("file:src/main/resources/order/");
}
});
context.start();
ProducerTemplate orderProducerTemplate = context.createProducerTemplate();
InputStream orderInputStream = new FileInputStream(ClassLoader.getSystemClassLoader()
.getResource("order.xml").getFile());
orderProducerTemplate.sendBody("direct:DistributeOrderDSL", orderInputStream);
} finally {
context.stop();
}
}
}
在main方法中,我們首先透過例項化DefaultCamelContext類中提供的預設實現來建立CamelContext。
CamelContext context = new DefaultCamelContext();
接下來,我們透過建立一個匿名的RouteBuilder例項來新增一條路由:
context.addRoutes(new RouteBuilder() {
我們重寫configure方法,以新增從直接 URI DistributeOrderDSL到系統控制檯的路由。我們使用 xpath 查詢提供一些過濾。
public void configure() throws Exception {
from("direct:DistributeOrderDSL")
.split(xpath("//order[@product = 'soaps']/items")).to("stream:out");
// .to("file:src/main/resources/order/");
}
新增路由後,我們啟動上下文:
context.start();
接下來,我們新增建立直接 URI - DistributeOrderDSL的程式碼。
ProducerTemplate orderProducerTemplate = context.createProducerTemplate();
InputStream orderInputStream = new FileInputStream(ClassLoader.getSystemClassLoader()
.getResource("order.xml").getFile());
最後,我們啟動處理:
orderProducerTemplate.sendBody("direct:DistributeOrderDSL", orderInputStream);
現在,您的 Java DSL 程式碼已完成,在測試應用程式之前唯一剩下的事情是將order.xml檔案新增到您的專案中。您可以為此目的使用介紹章節中顯示的示例 XML。
測試結果
執行應用程式時,您將看到以下輸出:
<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>
請注意,這裡只列出了肥皂訂單。如果您希望將其儲存到本地檔案,只需註釋掉stream.out行,並在您的configure方法中取消註釋以下行:
// .to("file:src/main/resources/order/");
在接下來的部分中,我們將學習如何將 Camel 與 Spring 整合使用。