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
RabbitMQ学习之spring配置文件rabbit标签的使用的更多相关文章
- (转)RabbitMQ学习之spring整合发送异步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...
- Spring学习笔记--Spring配置文件和依赖注入
Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...
- (转) 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配置文件beans标签报错问题解决
因为有很多配置是复制过来的,附带的很多注释的格式会导致报错,所以可以要试试把注释去掉,只有配置文件的话可能就不会报错了.
- Spring 源码(4)在Spring配置文件中自定义标签如何实现?
Spring 配置文件自定义标签的前置条件 在上一篇文章https://www.cnblogs.com/redwinter/p/16165274.html Spring BeanFactory的创建过 ...
- RabbitMQ学习之(二)_Centos6下安装RabbitMQ及管理配置
首先yum方式安装依赖包 yum install ncurses-devel unixODBC unixODBC-devel 安装Erlang语言环境 wget http://erlang.org/d ...
- (转)使用Spring配置文件实现事务管理
http://blog.csdn.net/yerenyuan_pku/article/details/52886207 前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Sprin ...
随机推荐
- Google Shell Style Guide
转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...
- Python中字符串操作函数string.split('str1')和string.join(ls)
Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换, 使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表 def split(self, * ...
- 1.sts的下载安装
sts的官方下载如下: http://spring.io/tools3/sts/all/ 将下载后的压缩文件解压,在解压后的sts-bundle下的sts-3.9.1RELEASE目录中STS.exe ...
- 【ACM】poj_2080_Calendar_201307311043
CalendarTime Limit: 1000MS Memory Limit: 30000K Total Submissions: 9787 Accepted: 3677 Description ...
- [bzoj1369][Baltic2003]Gem_树形dp_结论题
Gem bzoj-1369 Baltic-2003 题目大意:给你一棵树,让你往节点上添自然数,使得任意相邻节点的数不同且使得权值最小. 注释:n为结点个数,$1\le n\le 10^3$. 想法: ...
- Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容
Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容 当Git无法自动合并分支时,就必须首先解 ...
- LNMP一键安装包 V1.1 公布
LNMP一键安装包 是一个用Linux Shell编写的能够为CentOS/RadHat.Debian/Ubuntu VPS(VDS)或独立主机安装LNMP(Nginx.MySQL/MariaDB.P ...
- 2.【SELinux学习笔记】概念
1.强制类型的安全上下文 在SELinux中,訪问控制属性叫做安全上上下文.不管主体还是客体都有与之关联的安全上下文.通常安全上下文是由三部分组成:用户:角色:类型. 如: $id -Z j ...
- iOS GCD使用指南
Grand Central Dispatch(GCD)是异步运行任务的技术之中的一个. 一般将应用程序中记述的线程管理用的代码在系统级中实现.开发人员仅仅须要定义想运行的任务并追加到适当的Dispat ...
- nginx源代码分析--进程间通信机制 & 同步机制
Nginx源代码分析-进程间通信机制 从nginx的进程模型能够知道.master进程和worker进程须要通信,nginx中通信的方式有套接字.共享内存.信号.对于master进程,从外部接受信号, ...