Apache Camel uses URIs to work directly with any kind of Transport or messaging model such as HTTP, ActiveMQ, JMS, JBI, SCA, MINA or CXF, as well as pluggable Components and Data Format options. Apache Camel is a small library with minimal dependencies for easy embedding in any Java application. Apache Camel lets you work with the same API regardless which kind of Transport is used – so learn the API once and you can interact with all the Components provided out-of-box.
以上是引用 Apache Camel官网对它的定义。
Apache Camel 在编排模式中依托 URI 描述规则,实现了传输协议和消息格式的转换,即它具备协议转换,消息路由的能力。
白话翻译下Apache Camel功能:apache camel 能把业务数据封装在 Message 协议中,在各种不同的协议(如:HTTP,JMS,CXF等)中exchange下去。
接下来列举一个业务场景:
定时抓取百度页面内容,并存储到本地磁盘。
分析:1)百度提供的服务是http://www.baidu.com –> 这里的协议便是 http。
2)抓取百度的内容 –> 这里的内容就是数据,camel抓取到数据会把它封装进 Message,
3)抓取到的内容存储到本地磁盘 –> 本地磁盘的协议是 file, 存储下来涉及到内容的转移,即Message 的 Exchange.
简单代码实现:
package com.simonton.camel;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.junit.Test;
public class CamelDemoTest {
@Test
public void demo() throws Exception {
//Camel 的上下文对象 类似Sprin