日期:2014-05-16  浏览次数:20921 次

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" class="org.apache.activemq.camel.component.ActiveMQComponent">

<property name="connectionFactory">

<bean class="org.apache.activemq.ActiveMQConnectionFactory">

<property name="brokerURL"value="vm://testBroker?create=false&amp;waitForStart=1000"/>

<property name="userName" 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"/>