Apache Camel是个什么玩意?
初探Apache Camel
Apache Camel 是基于EIP(Enterprise Integration Patterns)的一款开源框架。适用于异构系统间的集成和处理数据。
核心
- Camel Context: Camel 上下文
- 为了运行和管理你的路由,Camel 有一个名为 Camel Context 的容器。您的路线在此引擎内运行。 你几乎可以把它想象成一个迷你应用服务器。
- 当 Camel启动时,它会读取您的路由定义(在 Java 或 XML 中),创建路由,将它们添加到 Camel 上下文,并启动 Camel 上下文。
- 当 Camel终止时,它会关闭您的路由,并关闭 Camel 上下文。
- Endpoint:用于收发消息的终端。
- Endpoint是Camel与其他系统进行通信的设定点。
- 使用URI来识别endpoint位置
- Exchange:消息之间通信的抽象的会话。
- Properties:Exchange对象贯穿整个路由执行过程中的控制端点、处理器甚至还有表达式、路由条件判断。为了让这些元素能够共享一些开发人员自定义的参数配置信息,Exchange以K-V结构提供了这样的参数配置信息存储方式。
- Message IN/OUT:当Endpoint和Processor、Processor和Processor间的Message在Exchange中传递时,Exchange会自动将上一个元素的输出作为这个元素的输入使用。
- Processor:消息处理器。
- org.apache.camel.Processor 是一个消息接受者和消息通信的处理器。
- Routing:路由规则。
- Routing用于处理Endpoint和Processor之间、Processor和Processor之间的路由跳转。
- components:组件库,也可视作endpoint工厂
-你可以在这里查看所有组件
如何创建Camel程序
java整合Apache Camel
`public class Test2 {
public static void main(String[] args) {
final CamelContext camelContext = new DefaultCamelContext();
try {
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from(“timer:initial//start?period=10000&repeatCount=7”)
.routeId(“timer–test”)
.to(“log:executed”);
}
});
} catch (Exception e) {
e.printStackTrace();
}
camelContext.setTracing(true);
camelContext.start();try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
camelContext.stop();
}
}`spring boot整合Apache Camel
- 添加maven配置
<dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>3.15.0</version> </dependency>
Camel自动配置会为您创建一个SpringCamelContext并负责该上下文的正确初始化和关闭。
Camel自动配置还从Spring上下文中收集所有RouteBuilder实例,并将它们自动注入到提供的CamelContext中。
- 设置自己的路由
- `package com.shuinfo.cameltest.route;
- 添加maven配置
import com.shuinfo.cameltest.process.Process1;
import org.apache.camel.builder.RouteBuilder;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MyCamelRoute3 extends RouteBuilder {
String say(){int i = RandomUtils.nextInt(1, 4);if (i == 1) {return "foo";}if (i == 2) {return "aaa";}if (i == 3) {return "bbb";}return "other";
}@Override
public void configure() throws Exception {from("timer:hello?period=10000").routeId("hello").transform().method("myCamelRoute3", "say").choice().when(simple("${body} contains 'foo'")).to("log:foo").when(simple("${body} contains 'aaa'")).to("log:aaa").otherwise().to("log:other").end();
}
}
`
使用实例
从文件夹1移动文件到文件夹2,并将文件内容记录日志
from("file:C:\\Users\\Administrator\\Desktop\\工作\\camel\\test1").log("捕获${body}").to("file:C:\\Users\\Administrator\\Desktop\\工作\\camel\\test2");
使用timer定时器,间隔10S(period=10000),执行5次
message信息转换为调用myCamelChoiceRoute3类,say()方法的返回值
根据Message body的值,选择不同的路由
@Component
public class MyCamelChoiceRoute3String say() {int i = RandomUtils.nextInt(1, 4);if (i == 1) {return "foo";}if (i == 2) {return "aaa";}if (i == 3) {return "bbb";}return "other";}}from("timer:hello?period=10000&repeatCount=5").routeId("hello").transform().method("myCamelChoiceRoute3", "say").choice().when(simple("${body} contains 'foo'")).to("log:foo").when(simple("${body} contains 'aaa'")).to("log:aaa").otherwise().to("log:other").end();
INFO 22:59:04 [Camel (camel) thread #2 - timer://hello] aaa CamelLogger:166: Exchange[ExchangePattern: InOnly, BodyType: String, Body: aaa]
INFO 22:59:14 [Camel (camel) thread #2 - timer://hello] foo CamelLogger:166: Exchange[ExchangePattern: InOnly, BodyType: String, Body: foo]
INFO 22:59:24 [Camel (camel) thread #2 - timer://hello] other CamelLogger:166: Exchange[ExchangePattern: InOnly, BodyType: String, Body: bbb]
INFO 22:59:34 [Camel (camel) thread #2 - timer://hello] aaa CamelLogger:166: Exchange[ExchangePattern: InOnly, BodyType: String, Body: aaa]
INFO 22:59:44 [Camel (camel) thread #2 - timer://hello] foo CamelLogger:166: Exchange[ExchangePattern: InOnly, BodyType: String, Body: foo]
camel提供了ProducerTemplate ConsumeTemplate来使POJO与camelRoute交互
@Produce("direct://test")ProducerTemplate producer;@RequestMapping(method = RequestMethod.GET, value = "/test")public String get(@RequestParam(value = "name") String name){producer.sendBody("<hello>"+name+"!</hello>");return name;}@Consume("direct://test")public void onCheese(String name) {// do something hereSystem.out.println(name);}
优缺点
优点
- 完全开源
- build in Java,可以拥有Java所有的功能
- 拥有庞大的组件库 拥有三百多种组件,且支持自定义
- DSL语言支持 不仅仅是XML还支持Java,Groovy和Scala DSL
缺点
- 学习成本
- 需要较高的编码能力(额,还有想象力)
使用场景
- 异构系统间的集成和交互
- 路由调度协调
- 消息分发
- 简易数据处理场景
Apache Camel是个什么玩意?相关推荐
- Apache Camel 2.15.0 发布,Java 规则引擎
Apache Camel 2.15.0 发布啦!!!该版本经过 6 个月开发,修复了超过 500 个 bug.但最值得关注的是给我们带来的新特性: 自文档 目录组件 Camel 工具类目录 重用 Ca ...
- Apache Camel框架入门示例
2019独角兽企业重金招聘Python工程师标准>>> Apache Camel是Apache基金会下的一个开源项目,它是一个基于规则路由和中介引擎,提供企业集成模式的Java对象的 ...
- 关于apache camel的消息转发效率
公司使用activemq和camel做消息的分发,之前数据量不是很大,所以一直没怎么考虑效率问题,对camel的工作原理研究也不深.单是最近随着业务量的增加,camel的效率逐渐成了瓶颈,所以根据日志 ...
- apache camel 的 split 和 aggregate
2019独角兽企业重金招聘Python工程师标准>>> split和aggregate,看图就明白了. 下面我用一个例子来说明,非常难得,你很难在网上找到apache camel这样 ...
- java camel dsl,Apache Camel与Spring DSL
我正在尝试使用spring DSL在Apache Camel中运行一个简单的应用程序 . 这是我的spring-config.xml 这是我的Java类测试: public class CamelSp ...
- Apache Camel简介与入门
Apache Camel 是一个基于知名的企业应用模式(Enterprise Integration Patterns)多功能的整合框架. StackOverflow上有很多学习Apache Came ...
- Apache Camel:基于企业集成模式(EIP)的开源集成框架
本资源由 伯乐在线 - 唐尤华 整理 Apache Camel 是一个功能强大的开源集成框架,基于企业集成模式(EIP)提供了强大的Bean集成功能. 介绍 通过Camel可以用企业集成模式创建路由和 ...
- 简化软件集成:一个Apache Camel教程
本文来自于阮一峰,文章主要讲解了构建的流程,每个步骤介绍的较为详细,希望对大家有帮助. 软件很少(如果有的话)存在于信息真空中.至少,这是我们软件工程师可以为我们开发的大多数应用程序做出的假设. 在任 ...
- Apache Camel框架之事务控制
http://blog.csdn.net/kkdelta/article/details/7249122 本文简单介绍一下Apache Camel如何对route进行事务控制,首先介绍整个route只 ...
最新文章
- Kubernetes日志分析利器:Elassandra部署使用指南
- 在mac上配置cocos2d-x开发环境
- 文件与目录管理——笔记
- 使用Heroku,解决gitment登录失败,报[object ProgressEvent]的错
- JS----JavaScript中的作用域和作用域链
- 阿里巴巴与小毛驴的故事——贪心算法
- xp3系统登录服务器错误,WindowsXP系统LOL服务器连接异常即将退出怎么解决?
- rest服务理解以及restful api
- Android CheckBoxPreference设置默认值会触发持久化以及其内部实现逻辑
- 大学四年Java学习路线规划,所有私藏资料我都贡献出来了,我要是早知道就好了
- 使用Mousetrap处理键盘快捷键(keypress.js和jQuery.hotkeys.js的使用)
- 7 基于matplotlib的python数据可视化——导入Excel数据制作饼图
- 【Leetcode】1925. Count Square Sum Triples
- penInfra峰会回归线下,彭博、沃尔沃、Adobe等用户将进行演讲交流
- 在 UltraEdit /UEStudio 中配置自动更正关键字
- 量子计算(1)量子力学基本理论(上)
- java版我的世界有溺尸_我的世界如何刷溺尸_minecraft溺尸陷阱制作教程 - 我的世界中文站...
- 以太坊:导入预售钱包,更新、备份、恢复账号
- 测试过程bug积累-搜索推荐
- #BDA#笔记#业务知识:常见行业业务模式指标4在线教育行业
热门文章
- JavaScript 中BOM及window的相关属性及方法
- div+css静态网页设计——海贼王动漫主题(6页) 影视主网页HTML代码 学生网页课程设计期末作业下载 动漫大学生网页设计制作成品下载 漫画网页作业代码下载
- 《数字图像处理》- 图像的输入,输出和显示
- 二叉树(类模板、函数模板、函数对象、函数指针)
- 分类——K-Means聚类分析
- yoga tab3 linux,全面进化联想YOGATab3Pro平板评测
- 【RTCP】malformed packet
- 【狂神说Java】SpringMVC最新教程IDEA版通俗易懂
- 桌面音乐linux,如何在Ubuntu桌面上使用Steam Music音乐播放器
- 开县影剧院座次_如何使用SteamVR的桌面剧院模式在VR中玩任何游戏