首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

11.7 使用Apache Camel路由发动机框架

2013-12-21 
11.7 使用Apache Camel路由引擎框架11.7 Routing engine with Apache Camel framework11.7 使用Apache Cam

11.7 使用Apache Camel路由引擎框架

11.7 Routing engine with Apache Camel framework

11.7 使用Apache Camel路由引擎框架

?

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

and applications.

?

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

?

At the core of the Camel framework is a routing engine builder. It allows you to

define your own routing rules, the sources from which to accept messages, and how to

process and send them to other destinations. Camel defines an integration language

that 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 by

using Apache Camel for some simple routing.

?

虽然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 we

can 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 configuration

file to include a Camel XML file with routing rules:

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

该配置文件包含了路由规则:

?

<beans>

<broker brokerName="testBroker">

<transportConnectors>

<transportConnector uri="tcp://localhost:61616"/>

</transportConnectors>

</broker>

<import resource="camel.xml"/>

</beans>

?

Note we call import resource after the broker definition to include a Camel ?XML

configuration file. Apache Camel comes with ?both a generic JMS component and ?a

more 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 ActiveMQ

broker 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://传输连接器时需要使用这个名称.

?

<bean id="activemq" value="DEFAULT_VALUE"/>

<property name="password" value="DEFAULT_VALUE"/>

</bean>

</property>

</bean>

?

We can now define a route. A useful enhancement is to tap into messages broadcast

on a topic, and place them in a queue for processing later:

下面我们可以定义路由了.一个比较有用的改进是使用消息主题广播消息,并将这些消息放入队列中,

以便在稍后再做处理.

?

<route>

<from uri="activemq:topic:Test.Topic"/>

<to uri="activemq:queue:Test.Queue"/>

</route>

?

This route will consume messages on the topic Test.Topic and route them to the

queue 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 be

useful to broadcast a message with statistical information periodically, and we can use

Apache Camel to do that.

?

现在,让我们解释一些更复杂的示例.代理的统计插件(statisticsBrokerPlugin)只会在收到请求时才发布统计消息.

因此,定期的广播统计消息是很有用的,下面我们使用Apache Camel来实现这个需求.

?

First, we need to ensure that the statisticsBrokerPlugin is enabled, as in the following

example configuration:

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

<beans>

<broker useJmx="false" persistent="false">

<plugins>

<statisticsBrokerPlugin/>

</plugins>

</broker>

<import resource="camel.xml"/>

</beans>

?

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. In

Apache Camel, a request/reply exchange is called InOut—we’ll poll the queue

named 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行代码,如下所示:

?

<route>

<from uri="timer://foo?fixedRate=true&amp;period=1000"/>

<inOut uri="activemq:queue:ActiveMQ.Statistics.DestinationTest.Queue"/>

<to uri="activemq:topic:Statistics.Topic"/>

</route>

?

Apache Camel is an extremely flexible and feature-rich framework. We’ve only

touched the surface of what you can achieve with it in conjunction with ActiveMQ. For

more information, we encourage you to read Camel in Action (Claus Ibsen and Jonathan

Anstey), available from Manning Publications.

?

Apache Camel是一种十分灵活且功能丰富的框架.我们仅仅涉及了Apache Camel联合ActiveMQ

应用的一些皮毛.更多关于Apache Camel的内容推荐你阅读<Camel in Action> (Claus Ibsen and Jonathan

Anstey著),可以从 Manning Publications中获取这本书.

?

热点排行