RabbitMQ学习之spring配置文件rabbit标签的使用
下面我们通过一个实例看一下rabbit的使用。
1.实现一个消息监听器ReceiveMessageListener.Java
- package org.springframework.amqp.core;
- /**
- * Listener interface to receive asynchronous delivery of Amqp Messages.
- *
- * @author Mark Pollack
- */
- public interface MessageListener {
- void onMessage(Message message);
- }
2.消费者配置Consumer.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:rabbit="http://www.springframework.org/schema/rabbit"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/rabbit
- http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
- <!-- 连接服务配置 -->
- <rabbit:connection-factory id="connectionFactory" host="192.168.36.102" username="admin"
- password="admin" port="5672" virtual-host="/" channel-cache-size="5" />
- <rabbit:admin connection-factory="connectionFactory"/>
- <!-- queue 队列声明-->
- <rabbit:queue durable="true" auto-delete="false" exclusive="false" name="spring.queue.tag"/>
- <!-- exchange queue binging key 绑定 -->
- <rabbit:direct-exchange name="spring.queue.exchange" durable="true" auto-delete="false">
- <rabbit:bindings>
- <rabbit:binding queue="spring.queue.tag" key="spring.queue.tag.key"/>
- </rabbit:bindings>
- </rabbit:direct-exchange>
- <bean id="receiveMessageListener"
- class="cn.slimsmart.rabbitmq.demo.spring.tag.ReceiveMessageListener" />
- <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象-->
- <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto" >
- <rabbit:listener queues="spring.queue.tag" ref="receiveMessageListener" />
- </rabbit:listener-container>
- </beans>
3.生产者配置Producer.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:rabbit="http://www.springframework.org/schema/rabbit"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/rabbit
- http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
- <!-- 连接服务配置 -->
- <rabbit:connection-factory id="connectionFactory"
- host="192.168.36.102" username="admin" password="admin" port="5672"
- virtual-host="/" channel-cache-size="5" />
- <rabbit:admin connection-factory="connectionFactory" />
- <!-- queue 队列声明 -->
- <rabbit:queue durable="true"
- auto-delete="false" exclusive="false" name="spring.queue.tag" />
- <!-- exchange queue binging key 绑定 -->
- <rabbit:direct-exchange name="spring.queue.exchange"
- durable="true" auto-delete="false">
- <rabbit:bindings>
- <rabbit:binding queue="spring.queue.tag" key="spring.queue.tag.key" />
- </rabbit:bindings>
- </rabbit:direct-exchange>
- <!-- spring amqp默认的是jackson 的一个插件,目的将生产者生产的数据转换为json存入消息队列,由于Gson的速度快于jackson,这里替换为Gson的一个实现 -->
- <bean id="jsonMessageConverter"
- class="cn.slimsmart.rabbitmq.demo.spring.tag.Gson2JsonMessageConverter" />
- <!-- spring template声明 -->
- <rabbit:template id="amqpTemplate" exchange="spring.queue.exchange" routing-key="spring.queue.tag.key"
- connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
- </beans>
4.消费者启动类ConsumerMain.java
- package cn.slimsmart.rabbitmq.demo.spring.tag;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class ConsumerMain {
- public static void main(String[] args) {
- new ClassPathXmlApplicationContext("Consumer.xml");
- }
- }
5.生产者启动类ProducerMain.java
- package cn.slimsmart.rabbitmq.demo.spring.tag;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class ProducerMain {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Producer.xml");
- AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
- User user = new User();
- user.setName("niuniu");
- amqpTemplate.convertAndSend(user);
- }
- }
先启动消费者,监听接收消息,再启动生产者发送消息。
输出: 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标签的使用相关推荐
- (转)RabbitMQ学习之spring整合发送异步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...
- spring配置文件import标签中使用${}占位符获得配置文件的属性值
2019独角兽企业重金招聘Python工程师标准>>> 一般情况下我们在Spring的配置文件中使用<import>标签是这样的,<import resource= ...
- (转) RabbitMQ学习之spring整合发送异步消息
http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...
- (转) RabbitMQ学习之spring整合发送同步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...
- (转)RabbitMQ学习之spring整合发送同步消息
http://blog.csdn.net/zhu_tianwei/article/details/40890543 以下实现使用Exchange类型为DirectExchange. routingke ...
- 五.解决Spring配置文件中标签不提示
写在前面,当我们在有网的情况下,不管我们的是项目是maven项目还是普通的项目,在编写Spring的配置文件Application.xml的时候不会出现标签没有提示的问题. 本文主要解决的是当不能上网 ...
- (转)使用Spring配置文件实现事务管理
http://blog.csdn.net/yerenyuan_pku/article/details/52886207 前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Sprin ...
- (转)使用Spring配置文件实现AOP
http://blog.csdn.net/yerenyuan_pku/article/details/52880558 使用Spring配置文件实现AOP 前面我们已经学会了使用Spring的注解方式 ...
- RabbitMQ学习之(二)_Centos6下安装RabbitMQ及管理配置
首先yum方式安装依赖包 yum install ncurses-devel unixODBC unixODBC-devel 安装Erlang语言环境 wget http://erlang.org/d ...
最新文章
- 推荐SpringBoot互联网企业级别的开源支付系统
- 加快LOOP嵌套循环的一个方法
- 怎样对流媒体进行压力测试_暖气片怎样安装效果好?暖气片正确的安装,采暖效果更好!...
- web前端面试题:20道做完信心嫉妒膨胀的测试题
- MySQL之父与企鹅的故事
- linux系统——fread()与read()函数族区别
- spring与junit整合测试
- 风格指南——Solidity中文文档(10)
- java缓存管理,一级缓存和二级缓存
- 开源游戏引擎哪家强?八款知名引擎资料够你忙
- 矩阵分析及应用(1-2章)
- matlab数据归一化函数mapminmax
- 运维网络设备监控平台搭建,网管平台 智能化网 管解决方案,实现智能拓扑管理
- easyCVR接入华为Vpaas(VCN/IVS) GB28181记录
- C#:实体框架EF(entity framework)
- 计算机基础图文混排教案,中职《Word图文混排》教学设计|word图文混排教学设计...
- 常用的几种RAID工作模式
- [批量主机存活扫描工具scanhost]扫描主机存活[python版本,非nmap版本]
- js实现正则表达式匹配
- 扫地机器人黑色耐脏吗_扫地机器人这么火?可你真的知道怎么选才不会入坑吗?...
热门文章
- (23)Verilog HDL条件语句:if-else语句
- FPGA积沙成塔(目录篇)
- 东师2016年秋季计算机基础,东师2016年秋季《计算机基础》期末考核答案(1).doc
- 用标准C语言初始化线性表,C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作...
- ubuntu jdk tomcat mysql_Ubuntu下安装JDK+TOMCAT+MYSQL
- STM32F103:一.(2)STLINK的配置
- libevent的线程优雅的退出方式
- 12011.linux之看门狗应用开发
- php 定义一个json对象,PHP中使用json数据格式定义字面量对象的方法
- 用函数模板实现选择排序算法_干货|STL容器和算法