下面我们通过一个实例看一下rabbit的使用。

1.实现一个消息监听器ReceiveMessageListener.Java

[java] view plaincopy print?
  1. package org.springframework.amqp.core;
  2. /**
  3. * Listener interface to receive asynchronous delivery of Amqp Messages.
  4. *
  5. * @author Mark Pollack
  6. */
  7. public interface MessageListener {
  8. void onMessage(Message message);
  9. }

2.消费者配置Consumer.xml

[html] view plaincopy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/rabbit
  12. http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
  13. <!-- 连接服务配置  -->
  14. <rabbit:connection-factory id="connectionFactory" host="192.168.36.102" username="admin"
  15. password="admin" port="5672" virtual-host="/"  channel-cache-size="5" />
  16. <rabbit:admin connection-factory="connectionFactory"/>
  17. <!-- queue 队列声明-->
  18. <rabbit:queue durable="true" auto-delete="false" exclusive="false" name="spring.queue.tag"/>
  19. <!-- exchange queue binging key 绑定 -->
  20. <rabbit:direct-exchange name="spring.queue.exchange" durable="true" auto-delete="false">
  21. <rabbit:bindings>
  22. <rabbit:binding queue="spring.queue.tag" key="spring.queue.tag.key"/>
  23. </rabbit:bindings>
  24. </rabbit:direct-exchange>
  25. <bean id="receiveMessageListener"
  26. class="cn.slimsmart.rabbitmq.demo.spring.tag.ReceiveMessageListener" />
  27. <!-- queue litener  观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象-->
  28. <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto" >
  29. <rabbit:listener queues="spring.queue.tag" ref="receiveMessageListener" />
  30. </rabbit:listener-container>
  31. </beans>

3.生产者配置Producer.xml

[html] view plaincopy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/rabbit
  11. http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
  12. <!-- 连接服务配置 -->
  13. <rabbit:connection-factory id="connectionFactory"
  14. host="192.168.36.102" username="admin" password="admin" port="5672"
  15. virtual-host="/" channel-cache-size="5" />
  16. <rabbit:admin connection-factory="connectionFactory" />
  17. <!-- queue 队列声明 -->
  18. <rabbit:queue  durable="true"
  19. auto-delete="false" exclusive="false" name="spring.queue.tag" />
  20. <!-- exchange queue binging key 绑定 -->
  21. <rabbit:direct-exchange name="spring.queue.exchange"
  22. durable="true" auto-delete="false">
  23. <rabbit:bindings>
  24. <rabbit:binding queue="spring.queue.tag" key="spring.queue.tag.key" />
  25. </rabbit:bindings>
  26. </rabbit:direct-exchange>
  27. <!-- spring amqp默认的是jackson 的一个插件,目的将生产者生产的数据转换为json存入消息队列,由于Gson的速度快于jackson,这里替换为Gson的一个实现 -->
  28. <bean id="jsonMessageConverter"
  29. class="cn.slimsmart.rabbitmq.demo.spring.tag.Gson2JsonMessageConverter" />
  30. <!-- spring template声明 -->
  31. <rabbit:template id="amqpTemplate" exchange="spring.queue.exchange"  routing-key="spring.queue.tag.key"
  32. connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
  33. </beans>

4.消费者启动类ConsumerMain.java

[java] view plaincopy print?
  1. package cn.slimsmart.rabbitmq.demo.spring.tag;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class ConsumerMain {
  4. public static void main(String[] args) {
  5. new ClassPathXmlApplicationContext("Consumer.xml");
  6. }
  7. }

5.生产者启动类ProducerMain.java

[java] view plaincopy print?
  1. package cn.slimsmart.rabbitmq.demo.spring.tag;
  2. import org.springframework.amqp.core.AmqpTemplate;
  3. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class ProducerMain {
  7. public static void main(String[] args) {
  8. ApplicationContext context = new ClassPathXmlApplicationContext("Producer.xml");
  9. AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
  10. User user = new User();
  11. user.setName("niuniu");
  12. amqpTemplate.convertAndSend(user);
  13. }
  14. }

先启动消费者,监听接收消息,再启动生产者发送消息。

输出: data :{"name":"niuniu"}

如下4中转发器类型标签

rabbit:fanout-exchange

rabbit:direct-exchange

rabbit:topic-exchange

rabbit:headers-exchange

参考:http://blog.csdn.net/michaelzhaozero/article/details/23741511

转载于:https://www.cnblogs.com/telwanggs/p/7124821.html

RabbitMQ学习之spring配置文件rabbit标签的使用相关推荐

  1. (转)RabbitMQ学习之spring整合发送异步消息(注解实现)

    http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...

  2. spring配置文件import标签中使用${}占位符获得配置文件的属性值

    2019独角兽企业重金招聘Python工程师标准>>> 一般情况下我们在Spring的配置文件中使用<import>标签是这样的,<import resource= ...

  3. (转) RabbitMQ学习之spring整合发送异步消息

    http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...

  4. (转) RabbitMQ学习之spring整合发送同步消息(注解实现)

    http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...

  5. (转)RabbitMQ学习之spring整合发送同步消息

    http://blog.csdn.net/zhu_tianwei/article/details/40890543 以下实现使用Exchange类型为DirectExchange. routingke ...

  6. 五.解决Spring配置文件中标签不提示

    写在前面,当我们在有网的情况下,不管我们的是项目是maven项目还是普通的项目,在编写Spring的配置文件Application.xml的时候不会出现标签没有提示的问题. 本文主要解决的是当不能上网 ...

  7. (转)使用Spring配置文件实现事务管理

    http://blog.csdn.net/yerenyuan_pku/article/details/52886207 前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Sprin ...

  8. (转)使用Spring配置文件实现AOP

    http://blog.csdn.net/yerenyuan_pku/article/details/52880558 使用Spring配置文件实现AOP 前面我们已经学会了使用Spring的注解方式 ...

  9. RabbitMQ学习之(二)_Centos6下安装RabbitMQ及管理配置

    首先yum方式安装依赖包 yum install ncurses-devel unixODBC unixODBC-devel 安装Erlang语言环境 wget http://erlang.org/d ...

最新文章

  1. 推荐SpringBoot互联网企业级别的开源支付系统
  2. 加快LOOP嵌套循环的一个方法
  3. 怎样对流媒体进行压力测试_暖气片怎样安装效果好?暖气片正确的安装,采暖效果更好!...
  4. web前端面试题:20道做完信心嫉妒膨胀的测试题
  5. MySQL之父与企鹅的故事
  6. linux系统——fread()与read()函数族区别
  7. spring与junit整合测试
  8. 风格指南——Solidity中文文档(10)
  9. java缓存管理,一级缓存和二级缓存
  10. 开源游戏引擎哪家强?八款知名引擎资料够你忙
  11. 矩阵分析及应用(1-2章)
  12. matlab数据归一化函数mapminmax
  13. 运维网络设备监控平台搭建,网管平台 智能化网 管解决方案,实现智能拓扑管理
  14. easyCVR接入华为Vpaas(VCN/IVS) GB28181记录
  15. C#:实体框架EF(entity framework)
  16. 计算机基础图文混排教案,中职《Word图文混排》教学设计|word图文混排教学设计...
  17. 常用的几种RAID工作模式
  18. [批量主机存活扫描工具scanhost]扫描主机存活[python版本,非nmap版本]
  19. js实现正则表达式匹配
  20. 扫地机器人黑色耐脏吗_扫地机器人这么火?可你真的知道怎么选才不会入坑吗?...

热门文章

  1. (23)Verilog HDL条件语句:if-else语句
  2. FPGA积沙成塔(目录篇)
  3. 东师2016年秋季计算机基础,东师2016年秋季《计算机基础》期末考核答案(1).doc
  4. 用标准C语言初始化线性表,C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作...
  5. ubuntu jdk tomcat mysql_Ubuntu下安装JDK+TOMCAT+MYSQL
  6. STM32F103:一.(2)STLINK的配置
  7. libevent的线程优雅的退出方式
  8. 12011.linux之看门狗应用开发
  9. php 定义一个json对象,PHP中使用json数据格式定义字面量对象的方法
  10. 用函数模板实现选择排序算法_干货|STL容器和算法