原文地址: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()
5         .to("ftp://192.168.101.3/camel-demo?username=admin&password=admin&binary=true");

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.

转载于:https://www.cnblogs.com/davidwang456/p/4482774.html

Getting started with Apache Camel--转载相关推荐

  1. Apache Camel 2.15.0 发布,Java 规则引擎

    Apache Camel 2.15.0 发布啦!!!该版本经过 6 个月开发,修复了超过 500 个 bug.但最值得关注的是给我们带来的新特性: 自文档 目录组件 Camel 工具类目录 重用 Ca ...

  2. Apache Camel框架入门示例

    2019独角兽企业重金招聘Python工程师标准>>> Apache Camel是Apache基金会下的一个开源项目,它是一个基于规则路由和中介引擎,提供企业集成模式的Java对象的 ...

  3. 关于apache camel的消息转发效率

    公司使用activemq和camel做消息的分发,之前数据量不是很大,所以一直没怎么考虑效率问题,对camel的工作原理研究也不深.单是最近随着业务量的增加,camel的效率逐渐成了瓶颈,所以根据日志 ...

  4. apache camel 的 split 和 aggregate

    2019独角兽企业重金招聘Python工程师标准>>> split和aggregate,看图就明白了. 下面我用一个例子来说明,非常难得,你很难在网上找到apache camel这样 ...

  5. linux反序列化漏洞,Apache Camel Java对象反序列化漏洞(CVE-2015-5348)

    Apache Camel Java对象反序列化漏洞(CVE-2015-5348) 发布日期:2015-12-15 更新日期:2015-12-18 受影响系统:Apache Group Camel 描述 ...

  6. Apache Camel,Spring Boot 实现文件复制,转移 (转)

    基本框架 Apache Camel Spring Boot Maven 开发过程 1.新建一个POM(quickstart)项目,在POM文件中添加Camel和Spring Boot的依赖 <p ...

  7. 架构设计:系统间通信(36)——Apache Camel快速入门(上)

    1.本专题主旨 1-1.关于技术组件 在这个专题中,我们介绍了相当数量技术组件:Flume.Kafka.ActiveMQ.Rabbitmq.Zookeeper.Thrift .Netty.DUBBO等 ...

  8. java camel dsl,Apache Camel与Spring DSL

    我正在尝试使用spring DSL在Apache Camel中运行一个简单的应用程序 . 这是我的spring-config.xml 这是我的Java类测试: public class CamelSp ...

  9. Apache Camel简介与入门

    Apache Camel 是一个基于知名的企业应用模式(Enterprise Integration Patterns)多功能的整合框架. StackOverflow上有很多学习Apache Came ...

  10. Apache Camel:基于企业集成模式(EIP)的开源集成框架

    本资源由 伯乐在线 - 唐尤华 整理 Apache Camel 是一个功能强大的开源集成框架,基于企业集成模式(EIP)提供了强大的Bean集成功能. 介绍 通过Camel可以用企业集成模式创建路由和 ...

最新文章

  1. MySQL 学习笔记(12)— 数据类型(定长字符、变长字符、字符串大对象、数字类型、日期时间类型、二进制类型)
  2. ​2018深度学习引用数最高的十大论文
  3. 转载一遍Java规范
  4. 在Nginx/Tengine服务器上安装SSL证书
  5. 创建unique时,约束和索引有何区别。唯一约束和唯一索引区别,选项忽略重复键作用
  6. iphone在jsp显示时间会NAN解决办法
  7. 渠道效果五步优化,让采购的流量物超所值
  8. VS学习笔记(一)创建C++项目
  9. typora的安装和配置
  10. java泛化_java 类字面常量,泛化的Class引用
  11. 根证书、服务器证书、用户证书的区别
  12. 美创科技出席世界信息安全大会:多维数据安全框架体系,护航新基建发展
  13. rtx3060性能相当于什么水平 rtx3060参数
  14. 整理了4大类22种图表,不用担心用错统计图表,分析不出东西了
  15. linux 电源管理源码分析,Linux 3.8.1 电源管理之OMAP Voltage Domain分析
  16. 网易七鱼的使用(web代码接入)
  17. 《查令十字街84号》读后感
  18. 迈克尔逊-莫雷实验新解
  19. 华为al00的计算机在哪,华为trt-al00是什么型号
  20. 手术室无菌注意事项的内容

热门文章

  1. python struct pack解析_python struct pack
  2. java logout_Java Core.logout方法代码示例
  3. sata接口_SATA接口成瓶颈 PCIe硬盘爆发: 群联主控增长120%
  4. 江苏信息考试access_2016年江苏省信息技术学业水平测试Access操作题
  5. 华为 鸿蒙只是物联网,“鸿蒙”不只是手机系统,任正非:是为物联网所打造的系统...
  6. 鼠标在linux下如何工作,Linux操作系统下的鼠标操作
  7. fiddler实现模拟器抓吧_使用Fiddler对手机抓包
  8. E: Unable to locate package libjasper-dev的解决办法(亲测可以解决)
  9. python关闭线程根据id_python之线程相关操作
  10. undefined control sequence_GeForce NOW在KDDI的帮助下提供服务:《Control》加入游戏库