`
m635674608
  • 浏览: 4942155 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

ActiveMQ与RabbitMQ使用camel集成

 
阅读更多

   著名的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的内容为:

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;"><beans  
  2.   xmlns="http://www.springframework.org/schema/beans"   
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xsi:schemaLocation="  
  5.     http://camel.apache.org/schema/springhttp://camel.apache.org/schema/spring/camel-spring.xsd  
  6.     http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.    
  8.    <camelContext id="camel"xmlns="http://camel.apache.org/schema/spring">  
  9.        <route>  
  10.            <description>Example Camel Route</description>  
  11.            <from uri="activemq:example.A"/>  
  12.            <to uri="activemq:example.B"/>  
  13.        </route>  
  14.    </camelContext>  
  15.    
  16.    <bean id="activemq"class="org.apache.activemq.camel.component.ActiveMQComponent" >  
  17.        <property name="connectionFactory">  
  18.          <beanclassbeanclass="org.apache.activemq.ActiveMQConnectionFactory">  
  19.            <property name="brokerURL"value="vm://localhost?create=true"/>  
  20.            <property name="userName"value="${activemq.username}"/>  
  21.            <property name="password" value="${activemq.password}"/>  
  22.          </bean>  
  23.        </property>  
  24.    </bean>  
  25. </beans></span>  


 3.        启动ActiveMQ后,浏览器输入http://localhost:8161/admin/queues.jsp,可以看到自动创建了一个队列example.A,并且加了一个消费者。

 

 

4.        点击Send To,发送一个消息到example.A,刷新页面,可以看到消息已经被转发:

  

二、ActiveMQ与RabbitMQ集成

详细的配置参数:http://camel.apache.org/rabbitmq.html

Ø  从RabbitMQ路由消息到ActiveMQ

1.        Camel里添加AMQP的路由如下:

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">  </span><route>  
  2.            <from uri="rabbitmq://localhost/t?username=guest&password=guest&exchangeType=topic&autoDelete=false&queue=t"/>  
  3.            <to uri="activemq:example.fromRMQ"/>  
  4.        </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中添加配置:

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <route>  
  2.     <from uri="activemq:test"/>  
  3.     <to uri="rabbitmq://localhost/?username=guest&password=guest&exchangeType=topic&autoDelete=false&queue=test"/>  
  4. </route>  

2.        重启ActiveMQ后,在控制台可以看到新增的队列test,

 

3.        往ActiveMQ的test队列发送一个消息。

4.        写个简单程序从Rabbitmq的队列test接收消息:

说明消息已经转发从ActiveMQ到RabbitMQ了。

各软件下载地址:

 

  1. 下载安装erlang:http://www.erlang.org/download.html
  2. 下载解压rabbitmq zip版本及java bin client zip:http://www.rabbitmq.com/download.html
  3. 添加控制台:http://www.rabbitmq.com/management.html
  4. Camel下载:http://camel.apache.org/download.html
  5. ActiveMQ下载:http://activemq.apache.org/download.html

 

 http://blog.csdn.net/kimmking/article/details/24427383

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics