Getting started with Apache Camel--转载
原文地址:http://www.javacodegeeks.com/2012/12/getting-started-with-apache-camel.html
About Camel:
Apache Camel is an open source project which is almost 5 years old and has a large community of users. At the heart of the framework is an engine which does the job of mediation and routes messages from one system to another. At the periphery it has got a plethora of components that allows interfacing with systems using various protocols (e.g. FTP(s), RPC, Webservices, HTTP, JMS, REST etc). Also it provides easy to understand domain specific language in Java, Spring and Scala.
Now let’s get started with Apache camel. We will set up a project using maven, add dependencies for required camel libraries and write our example using both Java and Spring DSL.
Consider a system that accepts two types of orders; widget and gadgets. The orders arrive on a JMS queue and are specified in XML format. The gadget inventory polls a file directory for incoming orders while the widget inventory listens on a queue. We run XPath on all arriving orders and figure out whether they belong to the widget or gadget inventory. Our use case is depicted by the following diagram:

To get started, just open a command line window in a directory and type mvn archetype:generate
1 |
"c:\myprojects>mvn archetype:generate |
assuming we have versions maven 2+ and jdk 1.6 in our path, also to run this example we need an activemq broker.
We will add the following dependencies in pom
01 |
org.apache.camel : camel-core : 2.10.1 |
02 |
- Lib containing Camel engine |
03 |
04 |
org.apache.camel : camel-ftp : 2.10.1 |
05 |
- Camel's ftp component |
06 |
07 |
org.apache.activemq : activemq-camel : 5.6.0 |
08 |
org.apache.activemq : activemq-pool : 5.6.0 |
09 |
- Libs required to integrate camel with activemq |
10 |
11 |
log4j : log4j : 1.2.16 |
12 |
org.slf4j : slf4j-log4j12 : 1.6.4 |
13 |
- Libs for logging |
Complete pom.xml is pasted on this gist entry.
Now lets code our camel route that shall poll a JMS queue, apply XPath to figure out whether the order is for gadget inventory or widget inventory and subsequently route it to FTP directory or a JMS queue.
Orders arriving in our system are having the below structure
01 |
<xml version="1.0" encoding="UTF-8"> |
02 |
<order> |
03 |
<product>gadget</product> |
04 |
<lineitems> |
05 |
<item>cdplayer</item> |
06 |
<qty>2</qty> |
07 |
</lineitems> |
08 |
<lineitems> |
09 |
<item>ipod-nano</item> |
10 |
<qty>1</qty> |
11 |
</lineitems> |
12 |
</order> |
Value of product element specifies whether is of gadget or it is a widget order. So applying below XPath on the orders shall let us decide where to route this message./order/product =’gadget’ then forward to an FTP directory else forward to a queue.
Now lets code the route, in order to do so one needs to extend the RouteBuilder (org.apache.camel.builder.RouteBuilder) class and override it’s configure method. We will name our class as JavaDSLMain and put the following code in its configure method:
1 |
from("activemq:queue:NewOrders?brokerURL=tcp://192.168.64.144:61616") |
2 |
.choice().when(xpath("/order/product = 'gadget'")) |
3 |
.to("activemq:queue:GadgetOrders?brokerURL=tcp://192.168.64.144:61616") |
4 |
.otherwise() |
Having done that, now lets analyze the above route. Keywords above form the Camel EIP DSL; the intent of this route is summarized as follows :
from : this says that get messages from an endpoint i.e consume, in our case this happens to be a queue.
choice : this is a predicate, here we apply a simple rule.
xpath: this says apply an xpath to the current message, the outcome of the xpath is a boolean.
to : this tells camel to place the message at an endpoint i.e. produce.
Each of this keyword may take some parameters to work. For example the from takes the endpoint parameter from which to consume messages, in our case it is a queue on a JMS (activemq) broker.
Notice that Camel automatically does type conversion for you, in the above route the message object is converted to a DOM before applying the XPath.
We will also put the main method in this class itself to quickly test this up. Inside the main method we need to instantiate a Camel context which shall host this route and on starting the context, Camel shall set up the route and start listening on the NewOrders queue.
The code that goes in the main method is as follows :
1 |
CamelContext camelContext = new DefaultCamelContext(); |
2 |
camelContext.addRoutes(new JavaDSLMain()); |
3 |
camelContext.start(); |
4 |
/* wait indefinitely */ |
5 |
Object obj = new Object(); |
6 |
synchronized (obj) { |
7 |
obj.wait(); |
8 |
} |
View this gist entry to get the complete code listing.
Another way to use Camel is with Spring, Camel routes goes into spring application context file. Instead of writing Java code all we use is XML to quickly define the routes. For doing this we need to import Camel namespaces in Spring context file and using
an IDE such as Spring tool suite one can quickly build & write integration applications.
Check this gist entry for the Spring application context demonstrating a Camel route.Place this context file inside META-INF/spring folder i.e in our maven project it goes under /src/main/resources/META-INF/spring folder.
At the top we have imported Camel’s Spring namespace which allows defining Camel route inside Spring’s application context , additionally in our pom file we need to add dependency to include Spring bean’s that takes care of recognizing and instantiating Camel engine inside Spring. Add below to include the Camel support for Spring.
1 |
<dependency> |
2 |
<groupId>org.apache.camel</groupId> |
3 |
<artifactId>camel-spring</artifactId> |
4 |
<version>2.10.1</version> |
5 |
</dependency> |
Camel provides a helper class (org.apache.camel.spring.Main) that scans for all Spring application context files kept under
META-INF/spring folder kept under classpath. Check this gist entry showing the required code.
With this example we have realized the Content Based Router pattern that inspects content of the message for routing decisions.
Getting started with Apache Camel--转载的更多相关文章
- Apache Camel,Spring Boot 实现文件复制,转移 (转)
基本框架 Apache Camel Spring Boot Maven 开发过程 1.新建一个POM(quickstart)项目,在POM文件中添加Camel和Spring Boot的依赖 <p ...
- Apache Camel 与 Spring Boot 集成,通过FTP定时采集、处理文件 (转)
1.概要: 本项目主要是通过在Spring平台上配置Camel.FTP,实现定时从FTP服务器下载文件到本地.解析文件.存入数据库等功能. 2.搭建空项目: Spring Boot有几种自动生成空项目 ...
- Apache Camel
Apache Camel 1 import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; i ...
- Apache Camel之FTP组件学习
写在最前面 哎,最近提了离职,手头的活也基本上清理的差不多了.想着这个把月可以舒服的晃悠晃悠的离开,但是运维的小伙伴总是不架势,走之前还是提了个新需求. 先说下需求吧,我们的系统概括的讲就是一个接口系 ...
- spring boot + apache camel 传输文件
一 sftp搭建略 这里简单说一下为什么使用sftp.ftp和sftp各有优点,差别并不是太大.sftp安全性好,性能比ftp低.ftp对于java来说并不复杂,效率也高.之所以使用sftp主要是可以 ...
- [每日一学]apache camel简介
apache camel 是轻量级esb框架.如下是它的架构图: 它有几个比较重要的概念就是: 1.endpoint,所谓的endpoint,就是一种可以接收或发送数据的组件.可以支持多种协议,如jm ...
- [每日一学]apache camel|BDD方式开发apache camel|Groovy|Spock
开发apache camel应用,最好的方式就是tdd,因为camel的每个组件都是相互独立并可测试的. 现在有很多好的测试框架,用groovy的Spock框架的BDD(行为测试驱动)是比较优秀和好用 ...
- Apache Camel继承Spring Boot 实现文件远程复制和转移
pom.xml <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-f ...
- apache camel 条件路由
<camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route id="e ...
- Centos 下搭建SVN + Apache 服务器(转载)
安装软件包 ? 1 # yum install httpd ? 1 # yum install mod_dav_svn ? 1 # yum install subversion 2. 验证安装 ? ...
随机推荐
- PHP 魔术方法总结
1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的 __get( $property ) 当调用一个未定义的属性时访问此方法 __set( $property, $va ...
- C#调用dll(C++(Win32))时的类型转换总结(转)
http://www.cnblogs.com/lidabo/archive/2012/06/05/2536737.html C++(Win 32) C# char** 作为输入参数转为char ...
- Spring学习笔记(一) Spring基础IOC、AOP
1. 注入类型 a) Spring_0300_IOC_Injection_Type b) setter(重要) c) 构造方法(可以忘记) d) ...
- 最全面的 MySQL 索引详解
什么是索引? 1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引,会保存在额外的文件中. 2.索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构 ...
- 软件工程 --- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试] [附加题]
软件工程 --- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试] [附加题] 首先,在分组之前,我和室友薛亚杰已经详细阅读了往届学长的博客,认为电梯调度 ...
- 值栈与ognl
ValueStack (值栈): 1.贯穿整个Action的生命周期(每个Action类的对象实例都拥有一个ValueStack对象).相当于一个数据的中转站.在其中保存当前Action对象和其他相关 ...
- jquery.loadmask.js
Quick Start 下载之后的目录结构如下图所示: 使用此插件非常简单,如下步骤所示: 1. 引用jquery,1.2.3以上版本 <script type="text/java ...
- 去掉 CONSOLE 窗口(转)
建立一个win32 console application的话,linker的/subsystem选项应该为CONSOLE,可以在VC开发环境的project->setting->link ...
- Unity3D之UGUI学习笔记(二):Rect Transform与Anchor
Rect Transform 我们都知道,Unity3D中所有的GameObject都必须要携带一个Transform组件,且该组件无法移除,那么作为UI显示的GameObject则不是携带Trans ...
- [Windows驱动开发](二)基础知识——数据结构
本节主要介绍驱动开发的一些基础知识. 1. 驱动程序的基本组成 1.1. 最经常见到的数据结构 a. DRIVER_OBJECT驱动对象 // WDK中对驱动对象的定义 // 每个驱动程序都会有一个唯 ...