Apache Camel简介与入门

本文介绍了Apache Camel的基础概念及入门教程,包括Message、Exchange、CamelContext等核心组件的作用和使用方法,同时展示了如何使用Java DSL定义路由,以及一个简单的文件复制示例。

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来查找端点。端点的功能强大、全面而且又可维护。来看一些例子。

?
1
2
3
4
5
6
//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。

示例路由:

?
1
2
//simple route.
from( "file:data/inbox" ).to( "jms:queue:order" )

路由可以使用过滤器、多播、接收列表、并行处理来定义,从而变得非常灵活。由于这篇文章只是简单的介绍Camel,我这里只给出一个注释的例子。这个使用了“direct:”架构,他提供了当消息生产者发出消息后直接的、同步的调用。

?
1
2
3
4
5
//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的一个元素,可用来消息格式转换或者其他的一些变换。

?
1
2
3
4
5
6
7
8
9
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" )

示例

使用maven创建一个Camel项目(Camel 架构概揽)。

?
1
mvn  archetype:generate -DgroupId=org.holbreich -DartifactId=filecopy -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-java -Dversion=1.0.0-SNAPSHOT

这个项目包换如下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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" );
   }
}

这个项目已经是一个可运行的了,赶紧试一试吧!

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值