Apache Camel

来源:互联网 发布:火鸟中文移动编程 编辑:程序博客网 时间:2024/05/19 06:16

1 什么是 camel ?

a mature open source project
an integration framework
focus on simplifying integration.

At the core of the Camel framework is a routing engine.
Camel makes no assumptions about the type of data you need to process.
Interact with various systems regardless of the protocol or data type the systems are using.

2 Camel 有哪些好东西?

  • Routing engine
  • Enterprise integration patterns (EIPs)
  • Domain-specific language (DSL)
  • Extensive component library
  • Payload-agnostic router — route any kind of payload
  • Modular and pluggable architecture
  • POJO model — Beans are first-class citizens
  • Easy configuration — intuitive URI configuration
  • Automatic type converters — built-in type-converter and custom converter
  • Lightweight core
  • Test kit
  • Vibrant community

3 第一个例子: 文件复制

将 srcFolder 文件夹下的所有文件复制到 dstFolder 文件夹下。

java 实现

public class FileCopyJavaImpl implements FileCopy {    @Override    public void fileCopy(String srcFolder, String dstFolder) throws IOException {        File inboxDirectory = new File(srcFolder);        File outboxDirectory = new File(dstFolder);        outboxDirectory.mkdir();        File[] files = inboxDirectory.listFiles();        for (File source : files) {            if (source.isFile()) {                File dest = new File(outboxDirectory.getPath() + File.separator + source.getName());                copyFIle(source, dest);            }        }    }    private void copyFIle(File source, File dest) throws IOException {        OutputStream out = new FileOutputStream(dest);        byte[] buffer = new byte[(int) source.length()];        FileInputStream in = new FileInputStream(source);        in.read(buffer);        try {            out.write(buffer);        } finally {            out.close();            in.close();        }    }}

Camel 实现 :

public class FileCopyCamelImpl implements FileCopy {    class MyRouteBuilder extends RouteBuilder {        @Override        public void configure() throws Exception {            from("file:data/inbox?noop=true").to("file:data/outbox");        }    }    @Override    public void fileCopy(String srcFolder, String dstFolder) throws Exception {        CamelContext context = new DefaultCamelContext();        context.addRoutes(new MyRouteBuilder());                context.start();        Thread.sleep(10000);        context.stop();    }}

需要 camel-core 的依赖

<dependency>    <groupId>org.apache.camel</groupId>    <artifactId>camel-core</artifactId>    <version>${camel-version}</version></dependency>

4 Camel 的消息模型

org.apache.camel.Message
org.apache.camel.Exchange

Message — 被路由的实体

body(payload) — 消息体
heads — 消息头,可多个
attachments — 附件,可选,用于 Camel-Mail 和 Web service 组件
fault flag — 错误标志

消息头是与消息相关的信息,如授权、编码、发送方ID等等。
消息头以键值对的形式存储在 Message 的一个 Map 中。
消息体的类型是:Object ,所以 Camel 可以路由任何格式的消息。

Exchange — 路由过程中消息的容器

  • Exchange ID — 该 exchange 的唯一标识
  • MEP — Message exchange pattern 消息交换模式: InOnly 或 InOut
  • In message — getIn()获得输入消息
  • Out message — getOut()获得输出消息
  • Exception — 错误处理
  • Properties — 功能与消息头类似,但它是全局的,而消息头特定于某个消息

5 Camel 的框架结构

CamelContext — 相当于 Camel 的运行时

CamelContext 提供了以下服务:

Components
Endpoints
Routes
Type Converters
Data formats
Registry — Spring ApplicationContext
Language — many different languages to create expressions.

Endpoint

An endpoint is the Camel abstraction that models the end of a channel through which a system can send or receive messages.

In Camel, you configure endpoints using Endpoint URIs.
By specifying a URI, you can identify the component you want to use and how that component is configured.
You can then decide to either send messages to the component configured by this URI, or to consume messages from it.

Processor

The processor represents a node capable of using, creating, or modifying an incoming exchange.

Route

Each route in Camel has a unique identifier that’s used for logging, debugging, monitoring, and starting and stopping routes.
Routes also have exactly one input source for messages, so they’re effectively tied to an input endpoint.

DSL

To wire processors and endpoints together to form routes, Camel defines a DSL.
In Camel, DSL means a fluent Java API that contains methods named for EIP terms.

java DSL 实例:

from("file:data/inbox").filter().xpath("/order[not(@test)]").to("jms:queue:order")

spring DSL 实例:

<route>    <from uri="file:data/inbox"/>    <filter>        <xpath>/order[not(@test)]</xpath>        <to uri="jms:queue:order"/>    </filter></route>

Component

components are associated with a name that’s used in a URI, and they act as a factory of endpoints.
For example, a FileComponent is referred to by file in a URI, and it creates FileEndpoints.

Producer

A producer is the Camel abstraction that refers to an entity capable of creating and sending a message to an endpoint.
When a message needs to be sent to an endpoint, the producer will create an exchange and populate it with data compatible with that particular endpoint.

Consumer

A consumer is the service that receives messages produced by a producer, wraps them in an exchange, and sends them to be processed.
Consumers are the source of the exchanges being routed in Camel.

An event-driven consumer waits idle until a message arrives, at which point it wakes up and consumes the message.
A polling consumer actively checks for new messages.

原创粉丝点击