定義:輕量級的集成框架,基于EIP(企業(yè)整合模式)blabla...
什么時候使用:
多個應(yīng)用程序使用不同的協(xié)議和技術(shù)集成,無論使用什么協(xié)議,無論使用什么技術(shù),無論特定領(lǐng)域的語言。
主要應(yīng)用場景:
1,消息匯聚,
比如你有來自不同服務(wù)器的消息,有ActiveMQ,RabbitMQ,WebService等,你想把它們都存儲到日志文件中
new RouteBuilder() {
@Override
public void configure() throws Exception {
from("amqp:queue:incoming").to("log:com.mycompany.log?level=DEBUG");
from("rabbitmq://localhost/A/routingKey=B").to("log:com.mycompany.log?level=DEBUG");
from("jetty:http://localhost:8080/myapp/myservice").to("log:com.mycompany.log?level=DEBUG");
}
}
from表示從這個endpoing取消息,to表示將消息發(fā)往這個endpoint,endpoint是消息地址,包含協(xié)議類型以及url。
2.消息分發(fā),
分為兩種,順序分發(fā)和并行分發(fā)。
順序分發(fā):先到第一個endpoint,處理完再分發(fā)到第二個endpoint......如果第一個endpoint出錯,那么消息不會下傳。
from("amqp:queue:order").to("uri:validateBean", "uri:handleBean", "uri:emailBean");
這個規(guī)則是從order隊列中取訂單信息,然后依次驗證訂單,處理訂單,并發(fā)送郵件通知用戶。任何一個步驟出錯,下一個步驟將不回執(zhí)行。
并行分發(fā):得到消息后同時分發(fā)到不同的endpoint,沒有先后順序之分,各個endpoint也是獨立處理消息的。如:
from("amqp:queue:order").multicast().to("uri:validateBean", "uri:handleBean", "uri:emailBean");
同時發(fā)送到to的所有endpoint。
3,消息轉(zhuǎn)換,如xml轉(zhuǎn)換json
from("amqp:queue:order").process(new XmlToJsonProcessor()).to("bean:orderHandler");
XmlToJsonProcessor是一個自定義的類,繼承org.apache.camel.Processor,用來將xml轉(zhuǎn)換為json。
4, 規(guī)則引擎
使用spring xml配置route,無需修改代碼,就能修改業(yè)務(wù)邏輯,解耦
如可以將from("amqp:queue:order").multicast().to("uri:validateBean", "uri:handleBean", "uri:emailBean");可以改成
<route>
<from uri="amqp:queue:order"/>
<multicast>
<to uri="uri:validateBean"/>
<to uri="uri:handleBean"/>
<to uri="uri:emailBean"/>
</multicast>
</route>
同時camel還內(nèi)置大量的processor,用于邏輯運算,過濾,這樣就容易靈活去route
from("amqp:queue:order").filter(header("foo").isEqualTo("bar")).choice()
.when(xpath("/person/city = 'London'"))
.to("file:target/messages/uk")
.otherwise()
.to("file:target/messages/others");
這條規(guī)則相對訂單進行過濾,只處理“bar”的訂單,然后根據(jù)城市講訂單給不同的endpoint