Jboss Fuse 简明教程
JBoss Fuse - AMQ With Camel
在本章中,我们将学习 ActiveMQ 与 Camel 配合使用的基础知识。
Configuring to ActiveMQ Component
在我们可以在代码中使用 ActiveMQ 队列或主题前,我们必须配置 ActiveMQComponent。ActiveMQComponent 的最小配置可以按照以下程序中所示完成 −
<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
<property name = "brokerURL" value = "tcp://localhost:61616"/>
<property name = "userName" value = "admin"/>
<property name = "password" value = "admin"/>
</bean>
-
brokerURL − 指定 AMQ Broker 的主机和端口。
-
username − 指定用于连接到 AMQ Broker 的用户名。
-
password − 指定用于连接到 AMQ Broker 的密码。
Connecting to Queue
既然我们已经配置了 ActiveMQComponent,我们可以将其作为端点在我们的 CamelContext 中使用。
我们将在以下格式中使用 AMQ 端点 −
Activemq:[queue|topic]:[queueName|topicName]
Writing Messages to AMQ
<?xml version = "1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
在 Fuse 容器中部署此捆绑包后,你应当能够看到已发布到 AMQ 的消息,这些消息作为文件放置在 D:/src/data 中。
Input
D:/src/data/input.txt
Test me
Output
Reading from AMQ
<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns = "http://camel.apache.org/schema/spring">
<!-- here is a sample which processes the input files
(leaving them in place - see the 'noop' flag)
then performs content based routing on the message using XPath -->
<route>
<from uri = "activemq:queue:TestQ"/>
<to uri = "file:///d:/src"/>
</route>
</camelContext>
<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
<property name = "brokerURL" value = "tcp://localhost:61616"/>
<property name = "userName" value = "admin"/>
<property name = "password" value = "admin"/>
</bean>
</beans>
Input
在部署此捆绑包后,你应当看到已在 D:/src 中生成文件且消息已被使用。该队列还应当显示使用者。
Output
D:/src
Test me
Writing to Topic
<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns = "http://camel.apache.org/schema/spring">
<!-- here is a sample which processes the input files
(leaving them in place - see the 'noop' flag)
then performs content based routing on the message using XPath -->
<route>
<from uri = "file:///d:/src"/>
<to uri = "activemq:topic:TestTopic” />
</route>
</camelContext>
<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
<property name = "brokerURL" value = "tcp://localhost:61616"/>
<property name = "userName" value = "admin"/>
<property name = "password" value = "admin"/>
</bean>
</beans>
Reading from Topic
<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns = "http://camel.apache.org/schema/spring">
<!-- here is a sample which processes the input files
(leaving them in place - see the 'noop' flag)
then performs content based routing on the message using XPath -->
<route>
<from uri = "activemq:topic:TestTopic"/>
<to uri = "file:///d:/src2"/>
</route>
</camelContext>
<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
<property name = "brokerURL" value="tcp://localhost:61616"/>
<property name = "userName" value = "admin"/>
<property name = "password" value = "admin"/>
</bean>
</beans>
Input
D:/src/file1.xml
<order>
<data>
<value>value1</value>
</data>
</order>
<order>
<data>
<value>value2</value>
</data>
</order>
<order>
<data>
<value>value3</value>
</data>
</order>
Output
D:/src/
<order>
<data>
<value>value1</value>
</data>
</order>
<order>
<data>
<value>value2</value>
</data>
</order>
<order>
<data>
<value>value3</value>
</data>
</order>