著名的EIP实现框架Camel最早起源于ActiveMQ内的一些基于消息的集成需求,然后逐渐发展成为一个ActiveMQ的子项目,最后这一块的功能越来越完善,就成为了Apache的顶级项目。
所以,从一开始到现在,ActiveMQ与Camel这两个项目一直都是紧密联系的,可以非常方便的整合使用:比如在ActiveMQ的配置文件中直接按照Spring的配置方式使用Camel来实现ActiveMQ与其他外部系统或中间件的集成。而ActiveMQ中的一些简单的集成功能也越来越倾向于直接去掉或者移植到Camel环境中去实现。
环境:ActiveMQ 5.9.0、RabbitMQ3.3.0、
一、ActiveMQ与ActiveMQ的集成
实现一个简单的从一个Queue到另一个Queue的消息转发。
1. 在activemq.xml中加一句:<import resource="camel.xml"/>
2. 加一个简单路由,从队列example.A转发消息到队列example.B,camel.xml的内容为:
- <span style="font-size:14px;"><beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://camel.apache.org/schema/springhttp://camel.apache.org/schema/spring/camel-spring.xsd
- http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
- <camelContext id="camel"xmlns="http://camel.apache.org/schema/spring">
- <route>
- <description>Example Camel Route</description>
- <from uri="activemq:example.A"/>
- <to uri="activemq:example.B"/>
- </route>
- </camelContext>
- <bean id="activemq"class="org.apache.activemq.camel.component.ActiveMQComponent" >
- <property name="connectionFactory">
- <beanclassbeanclass="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL"value="vm://localhost?create=true"/>
- <property name="userName"value="${activemq.username}"/>
- <property name="password" value="${activemq.password}"/>
- </bean>
- </property>
- </bean>
- </beans></span>
3. 启动ActiveMQ后,浏览器输入,可以看到自动创建了一个队列example.A,并且加了一个消费者。
4. 点击Send To,发送一个消息到example.A,刷新页面,可以看到消息已经被转发:
二、ActiveMQ与RabbitMQ集成
详细的配置参数:
Ø 从RabbitMQ路由消息到ActiveMQ
1. Camel里添加AMQP的路由如下:
- <span style="white-space:pre"> </span><route>
- <from uri="rabbitmq://localhost/t?username=guest&password=guest&exchangeType=topic&autoDelete=false&queue=t"/>
- <to uri="activemq:example.fromRMQ"/>
- </route>
2. 复制camel中的camel-rabbitmq-2.13.0.jar 和rabbitmq-java-client中的rabbitmq-client.jar到apache-activemq-5.9.0\lib\camel下。
3. 重启ActiveMQ,在rabbitmq的控制台可以看到自动创建的exchange为t,
4. 在rabbitmq控制台向t中发送消息,
5. 刷新ActiveMQ控制台可以看到消息已经从rabbitmq路由到activemq:
Ø 从ActiveMQ路由消息到Rabbitmq
1. 在camel.xml中添加配置:
- <route>
- <from uri="activemq:test"/>
- <to uri="rabbitmq://localhost/?username=guest&password=guest&exchangeType=topic&autoDelete=false&queue=test"/>
- </route>
2. 重启ActiveMQ后,在控制台可以看到新增的队列test,
3. 往ActiveMQ的test队列发送一个消息。
4. 写个简单程序从Rabbitmq的队列test接收消息:
说明消息已经转发从ActiveMQ到RabbitMQ了。
各软件下载地址:
- 下载安装erlang:
- 下载解压rabbitmq zip版本及java bin client zip:
- 添加控制台:
- Camel下载:
- ActiveMQ下载:
http://blog.csdn.net/kimmking/article/details/24427383