JBoss Fuse - AMQ 与 Camel
在本章中,我们将学习 ActiveMQ 如何与 Camel 配合使用的基础知识。
配置 ActiveMQ 组件
在代码中使用 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 的密码。
连接到队列
现在我们已经配置了 ActiveMQComponent,我们可以在 CamelContext 中将其用作端点。
我们将使用以下格式的 AMQ 端点 −
Activemq:[queue|topic]:[queueName|topicName]
将消息写入 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 中。
输入
D:/src/data/input.txt
Test me
输出

从 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>
输入
部署此包后,您应该会看到在 D:/src 中生成一个文件,并且消息已被使用。还应该显示该队列的消费者。

输出
D:/src
Test me
写入主题
<?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>
从主题读取
<?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>
输入
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>
输出
D:/src/
<order> <data> <value>value1</value> </data> </order> <order> <data> <value>value2</value> </data> </order> <order> <data> <value>value3</value> </data> </order>