当前位置: 首页 > ActiveMQ in Action 读书笔记 > 正文

11.7 使用 Apache Camel 路由引擎框架

11.7 Routing engine with Apache Camel framework

Apache Camel is a simple-to-use integration framework that’s easily embedded in containersand applications.

11.7 使用 Apache Camel 路由引擎框架

Apache Camel 是一个非常易于使用的集成框架,很容易集成到容器或者应用程序中.

At the core of the Camel framework is a routing engine builder. It allows you todefine your own routing rules, the sources from which to accept messages, and how toprocess and send them to other destinations. Camel defines an integration languagethat allows you to define routing rules, akin to business processes.

Camel框架的核心是一个路由引擎构建器.Camel允许你定义自己的路由规则,即,定义从什么地方接收消息,以及如何处理和发送消息到其他消息目的地.Camel定义了一种集成语言,允许你用与定义业务逻辑一样的方式定义路由规则.

Although Apache Camel supports a large number of integration components,we’ll demonstrate some relatively simple ways to extend the power of ActiveMQ byusing Apache Camel for some simple 

虽然 Apache Camel 支持大量的集成组建,但是我们只介绍一些与ActiveMQ相关的基本方式以扩展ActiveMQ代理的普通路由能力.

Camel can use either a Java-based domain-specific language (DSL), or Scala DSL,or an XML-based DSL to define routes. We’ll concentrate on the XML-based DSL, so wecan extend an ActiveMQ broker functionality directly from an XML configuration file.Apache Camel uses simple English prepositions, such as from and to, to denote a route.

Camel可以使用基于Java的领域特定语言(DSL),或Scala DSL 或者基于XML的DSL来定义路由.我们将集中介绍基于XML的DSL,因而我们可以直接通过一个XML配置文件来扩展ActiveMQ代理的功能. Apache Camel 使用简单的英语介词,比如from和to来配置路由.

It’s easy to explain with an example. First we’ll define a simple broker configurationfile to include a Camel XML file with routing rules:

使用一个示例来解释最简单明了.首先,我们定义一个简单的代理配置文件以引入Camel的XML配置文件,该配置文件包含了路由规则:

Note we call import resource after the broker definition to include a Camel  XMLconfiguration file. Apache Camel comes with  both a generic JMS component and  amore specific, optimized  ActiveMQ component. Obviously  we’ll use the  latter.

注意,我们在配置完代理后,使用import来引入了Camel的XML配置文件.Apache Camel的带有两个通用JMS组件和一个更具体地.优化过的ActiveMQ组件.显然我们将是有优化过的后者.

The ActiveMQ component  needs to be  configured to communicate  with the broker,and we’ll  use the  vm:// transport  to do  this. Note  we called  the ActiveMQbroker testBroker, so this needs to be the name we use when we set up the  vm://transport in the Camel XML configuration file:

ActiveMQ组建需要配置成可以和代理通信,我们使用vm://传输连接器来配置.注意,我们将ActiveMQ代理命名为testBroker,因此在Camel的XML配置文件中设置vm://传输连接器时需要使用这个名称.

We can now define a route. A useful enhancement is to tap into messages broadcaston a topic, and place them in a queue for processing later: 下面我们可以定义路由了.一个比较有用的改进是使用消息主题广播消息,并将这些消息放入队列中,以便在稍后再做处理.

This route will consume messages on the topic Test.Topic and route them to thequeue Test.Queue. Simple, but useful stuff.

上面的路由配置将处理主题Test.Topic中的消息,并将它们路由到队列Test.Queue中.虽然简单,但是很有用的处理.

Let’s demonstrate something more complex. The statistics broker plug-in(statisticsBrokerPlugin) will only publish a statistic message when requested. So it’d beuseful to broadcast a message with statistical information periodically, and we can useApache Camel to do that.

现在,让我们解释一些更复杂的示例.代理的统计插件(statisticsBrokerPlugin)只会在收到请求时才发布统计消息.因此,定期的广播统计消息是很有用的,下面我们使用Apache Camel来实现这个需求.

First, we need to ensure that the statisticsBrokerPlugin is enabled, as in the followingexample configuration:

首先,我们需要保证statisticsBrokerPlugin插件已经启用了.可以使用下面的示例配置启用插件:

Then, with Apache Camel, we’ll do the following:
? Use the timer component to initiate the name of the route to poll.
? Communicate with the statistics plug-in using a request/reply pattern. InApache Camel, a request/reply exchange is called InOut—we’ll poll the queuenamed Test.Queue.
? Broadcast the result on a topic called Statistics.Topic.

接下来,借助与Apache Camel,我们需要完成一下几个步骤:
(1) 使用计时组件初始化路由的名称以便轮询
(2)与统计插件使用请求/应答模式进行通信.Apache Camel中的请求/应答交互被成为InOUt–我们将轮询名称为Test.Queue的消息队列.
(3)广播结果到名称为Statistics.Topic的主题

The complete Apache Camel route is only three lines of code, as shown:

完成的Apache Camel路由配置仅有3行代码,如下所示:

Apache Camel is an extremely flexible and feature-rich framework. We’ve onlytouched the surface of what you can achieve with it in conjunction with ActiveMQ. Formore information, we encourage you to read Camel in Action (Claus Ibsen and JonathanAnstey), available from Manning Publications.

Apache Camel是一种十分灵活且功能丰富的框架.我们仅仅涉及了Apache Camel联合ActiveMQ应用的一些皮毛.更多关于Apache Camel的内容推荐你阅读<Camel in Action> (Claus Ibsen and JonathanAnstey著),可以从 Manning Publications中获取这本书.

打个赏呗

   微信打赏  支付宝打赏


本文固定链接: https://www.jack-yin.com/coding/translation/activemq-in-action/1732.html | 边城网事

该日志由 边城网事 于2013年12月18日发表在 ActiveMQ in Action 读书笔记 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 11.7 使用 Apache Camel 路由引擎框架 | 边城网事

11.7 使用 Apache Camel 路由引擎框架 暂无评论

发表评论

快捷键:Ctrl+Enter