Apache Camel簡介與入門
Apache Camel 是一個基于知名的企業應用模式(Enterprise Integration Patterns)多功能的整合框架.
StackOverflow上有很多學習Apache Camel的資源,而這里僅僅是使用一個實例來簡單的介紹一下Apache Camel
基礎
Apache Camel十一個Java庫和引擎,有多種不同的整合模式,然而他并不是BPMN或者ESB,雖然可以在此引擎下實現他們。Apache Camel是一個編程人員調節、整合問題的工具。
Message
org.apache.camel.Message是Camel中一個基本的包含數據和路由的實體,Messages包含了
- 唯一的識別(Unique Identifier)--java.lang.String類型
- 頭信息(Headers)--會提供一些內容的提示,頭信息被組織成名值對的形式,string-->Object
- 內容(body)是一個Object類型的對象,這就意味著,你要確保接收器能夠理解消息的內容。當消息發送器和接收器使用不同的內容格式的時候,你可以使用Camel的數據轉換機制將其轉換為一個特定的格式。在許多情況下預先定義類型可以被自動轉換。
- 錯誤標記(fault flag)使用來標記正常或者錯誤的標記,通常由一些標準類定義,例如(WSDL)
Exchange
org.apache.camel.Exchange 是一個消息之間通信的抽象的會話。下面列出的就是這樣一個會話,使得組件更為全面
- Exchange ID
- MEP 一個類似InOnly或者InOut的消息交換模式。當模式是InOnly的時候,消息交換中只包含IN-Message
- Exception在路由過程中的任何異常
- Properties有點類似與message 的headers ,但是他們將持續到整個exchange結束,Camel還可能利用他們進行一些特殊的通信。
- IN-Message
- OUT-Message
Camel Context
現在讓我們來看看一張圖,我們看到的是一些不同的相互鏈接的構件,而在他們中間起鏈接作用的粘合劑就是Camel Context了。他將實體鏈接一起,有的時候被稱為Camel運行是容器。
Endpoint
是Camel中的一個基本概念,Endpoint作為Camel系統中一個通道的端點,可以發送或者接受消息。在Camel中Endpoint使用URI來配置。在運行時Camel通過URI來查找端點。端點的功能強大、全面而且又可維護。來看一些例子。
//Pooling on data/inbox every 2 seconds file:data/inbox?delay=2000 //JMS queendpoid with name order jms:queue:order //Run's external Application with output.txt as parameter. exec:archiver.exe?output.txt
Component
Component是一些Endpoints URI的集合。他們通過連接碼來鏈接(例如file:,jms:),而且作為一個endpoint的工廠。現在Camel中又超過80個Component。當然你一可以通過擴展org.apache.camel.impl.DefaultComponent來實現自己的Component
Route
顧名思義,Route,就是路由,它定義了Message如何在一個系統中傳輸的真實路徑或者通道。路由引擎自身并不暴露給開發者,但是開發者可以自己定義路由,并且需要信任引擎可以完成復雜的傳輸工作。每個路由都有一個唯一的標識符,用來記錄日志、調試、監控,以及啟動或者停止路由。
路由也有一個輸入的Message,因此他們也有效的鏈接到一個輸入端點。路由定義了一種領域特有的語言(DSL)。Camel提供了java、scala和基于XM的Route-DSL。
示例路由:
//simple route. from("file:data/inbox").to("jms:queue:order")
路由可以使用過濾器、多播、接收列表、并行處理來定義,從而變得非常靈活。由于這篇文章只是簡單的介紹Camel,我這里只給出一個注釋的例子。這個使用了“direct:”架構,他提供了當消息生產者發出消息后直接的、同步的調用。
//Every 10 seconds timer sends an Exchange to direct:prepare from("timer://foo?fixedRate=true&period=10000").to("direct:prepare"); // Onother Routes can begin from "direct:prepare" // This now depends on timer, logging and putting a message to the queue. from(direct:prepare).to("log:com.mycompany.order?level=DEBUG").to("jms:queue:order?jmsMessageType=Text");
Processor
org.apache.camel.Processor 是一個消息接受者和消息通信的處理器。當然,Processor是Route的一個元素,可用來消息格式轉換或者其他的一些變換。
Processor myProcessor = new Processor() { public void process(Exchange exchange) { exchange.getBody(); //e.g do something with Body.. } };from("file:data/inbox").filter(header("foo").isEqualTo("bar")) .process(myProcessor).to("jms:queue:order")</pre>
示例
使用maven創建一個Camel項目(Camel 架構概攬)。
mvn archetype:generate -DgroupId=org.holbreich -DartifactId=filecopy -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-java -Dversion=1.0.0-SNAPSHOT
這個項目包換如下代碼:
package org.holbreich.filecopy; import org.apache.camel.main.Main;public class MainApp { /**
- A main() so we can easily run these routing rules in our IDE */ public static void main(String... args) throws Exception { Main main = new Main(); main.enableHangupSupport(); main.addRouteBuilder(new MyRouteBuilder()); main.run(args); } } and package org.holbreich.filecopy; import org.apache.camel.builder.RouteBuilder;
/**
- A Camel Java DSL Router */ public class MyRouteBuilder extends RouteBuilder {
/**
Let's configure the Camel routing rules using Java code... */ public void configure() { // here is a sample which processes the input files // (leaving them in place - see the 'noop' flag) // then performs content based routing on the message using XPath from("file:src/data?noop=true")
.choice() .when(xpath("/person/city = 'London'")) .to("file:target/messages/uk") .otherwise() .to("file:target/messages/others");
} }</pre>
這個項目已經是一個可運行的了,趕緊試一試吧!
OSChina.NET原創翻譯/原文鏈接