RabbitMQ九 RabbitMQ入门教程 - 与Spring集成

RabbitMQ入门教程 For Java【9】 - 与Spring集成



简介:

        RabbitMQ在与Spring集成我做了两个项目,一个项目是消息生产者,负责发送消息,另外一个是消息消费者,负责监听消息。大致的流程图如下:



项目环境:

Windows7 64bit

Eclipse Kepler SR2

JDK 1.7

Tomcat 7

RabbitMQ 3.6.0

项目源码地址:

生产者:https://github.com/chwshuang/spring-rabbitmq-producer

消费者:https://github.com/chwshuang/spring-rabbitmq-customer

 

生产者:

        与Spring集成的项目使用的是Maven,只需要一个依赖配置就搞定:

[plain] view plain copy

1.               <!-- RabbitMQ -->  

2.  lt;dependency>  

3.  <groupId>org.springframework.amqp</groupId>  

4.  <artifactId>spring-rabbit</artifactId>  

5.  <version>1.3.5.RELEASE</version>  

6.  lt;/dependency>  


        
消息生产者在Spring的配置也比较简单,只需要一个连接工厂和一个连接模版类就搞定了。

[plain] view plain copy

1.         <!-- ========================================RabbitMQ========================================= -->  

2.  <rabbit:connection-factory id="connectionFactory" host="localhost" publisher-confirms="true" virtual-host="test" username="test" password="1234" />  

3.    

4.  <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>  


       生产者在Spring中使用时,只需要定义一个服务接口实现类即可。

[plain] view plain copy

1.  @Service("rabbitService")  

2.  public class RabbitServiceImpl {  

3.      private static Logger log = LoggerFactory.getLogger(RabbitServiceImpl.class);  

4.      @Autowired  

5.      private RabbitTemplate rabbitTemplate;  

6.    

7.      /**  

8.       * 发送消息  

9.       * @param msg 消息内容  

10.      * @param routingKey 路由关键字  

11.      * void  

12.      */  

13.     public void setMessage(String msg, String routingKey) {  

14.         rabbitTemplate.convertAndSend(routingKey, msg);  

15.         log.info("rabbitmq--发送消息完成: routingKey[{}]-msg[{}]", routingKey, msg);  

16.     }  

17. }  

 

生产者端还需要创建一个测试页面,通过Ajax技术发送消息到生产者端的控制层,由控制层调用消息服务层发送消息。

[html] view plain copy

1.  <%@ page contentType="text/html;charset=UTF-8" language="java"%>  

2.  <!DOCTYPE html>  

3.  <html>  

4.  <head>  

5.  <meta charset="UTF-8">  

6.  <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>  

7.  <title>测试</title>  

8.  <script type="text/javascript">  

9.   function sendMsg(){  

10.      var t = $("#type").val();  

11.      //alert(t);  

12.      $.post('/producer/rabbit/setMessage',{msg:'hello world '+t+' rabbit!',type:t}, function(result){  

13.          if(result.success){  

14.              //alert("发送成功");  

15.          }else{  

16.              alert("发送失败");  

17.          }  

18.      },'json');  

19.  }  

20. </script>  

21. </head>  

22. <body>  

23. <h1>发送消息</h1><hr>  

24. <select id="type" >   

25. <option selected="selected" value="red">red</option>  

26. <option value="blue">blue</option>  

27.   

28. </select>  

29. <button id="send" onclick="sendMsg()" value="发送消息" type="button" title="send">发送</button>  

30. </body>  

31. </html>  




 

消费者:

        RabbitMQ的消息消费者需要监听消息,以及处理收到的消息,所以需要配置一个监听器,声明一个需要监听的队列,和一个消息处理器。消费者端与spring集成的侵入较少。

[plain] view plain copy

1.  <pre name="code" class="plain">   <!-- ========================================RabbitMQ========================================= -->  

2.      <!-- 连接工厂 -->  

3.      <rabbit:connection-factory id="connectionFactory" host="192.168.5.198" publisher-confirms="true" virtual-host="test" username="test" password="1234" />  

4.      <!-- 监听器 -->  

5.      <rabbit:listener-container connection-factory="connectionFactory">  

6.          <!-- queues是队列名称,可填多个,用逗号隔开, methodref指定的Bean调用Invoke方法执行的方法名称 -->  

7.          <rabbit:listener queues="red" method="onMessage" ref="redQueueListener" />  

8.          <rabbit:listener queues="blue" method="onMessage" ref="blueQueueListener" />  

9.      </rabbit:listener-container>  

10.     <!-- 队列声明 -->  

11.     <rabbit:queue name="red" durable="true" />  

12.     <!-- 队列声明 -->  

13.     <rabbit:queue name="blue" durable="true" />  

14.     <!-- 红色监听处理器 -->  

15.     <bean id="redQueueListener" class="com.aitongyi.customer.RedQueueListener" />  

16.     <!-- 蓝色监听处理器 -->  

17.     <bean id="blueQueueListener" class="com.aitongyi.customer.BlueQueueListener" />  




        消息处理器可以只是一个通过spring管理的普通Bean对象,需要有一个在xml中定义method同名的方法

[plain] view plain copy

1.  public class RedQueueListener{  

2.      private static Logger log = LoggerFactory.getLogger(RedQueueListener.class);  

3.      /**  

4.       * 处理消息  

5.       * @param message  

6.       * void  

7.       */  

8.      public void onMessage(String message) {  

9.          log.info("RedQueueListener--receved:"  + message);  

10.     }  

11. }  




RabbitMQ创建消息环境

 

        在项目开发完成后,我们需要在RabbitMQ中创建vhost,user、queues,这样,消费者在启动的时候,登录、声明虚拟机、队列就不会报错。

1. 创建test虚拟机,然后点击Name标签,进入虚拟机,添加权限

 

 

2. 进入【Admin】菜单【Users】标签创建用户,然后点击用户名称进入详情设置权限

 

 

 

3. 进入【Queues】菜单,创建队列

 

 

执行测试:

将生产者和消费者项目添加到Tomcat中,然后启动,在浏览器输入【http://localhost:8080/producer/test.jsp】,进入测试页面,分别发送红色、蓝色的消息到队列,消费者端会显示接收日志

生产者端日志

[plain] view plain copy

1.  2016-01-25 18:09:26 722 [INFO] c.a.p.c.RabbitController - rabbitmq--收到待发送消息: type[blue]-msg[hello world blue rabbit!]  

2.  2016-01-25 18:09:26 723 [INFO] c.a.p.s.RabbitServiceImpl - rabbitmq--发送消息完成: routingKey[blue]-msg[hello world blue rabbit!]  

3.  2016-01-25 18:09:28 715 [INFO] c.a.p.c.RabbitController - rabbitmq--收到待发送消息: type[red]-msg[hello world red rabbit!]  

4.  2016-01-25 18:09:28 716 [INFO] c.a.p.s.RabbitServiceImpl - rabbitmq--发送消息完成: routingKey[red]-msg[hello world red rabbit!]  


消费者端日志

[plain] view plain copy

1.  2016-01-25 18:09:26 727 [INFO] c.a.c.BlueQueueListener - BlueQueueListener Receved:hello world blue rabbit!  

2.  2016-01-25 18:09:28 719 [INFO] c.a.c.RedQueueListener - RedQueueListener Receved:hello world red rabbit!  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值