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 ...
随机推荐
- Codeforces Round #406 (Div. 2) 787-D. Legacy
Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So ...
- 06.系统编程-4.多线程和GIL
为什么有人会说 Python? 多线程是鸡肋?知乎上有人提出这样一个问题,在我们常识中,多进程.多线程都是通过并发的方式充分利用硬件资源提高程序的运行效率,怎么在 Python 中反而成了鸡肋? 有同 ...
- Python列表的复制
1.直接按名字赋值: my_habit = ['game', 'running'] friend_habit = my_habit my_habit.append('swimming') friend ...
- lucene_01_入门程序
索引和搜索流程图: 1.绿色表示索引过程,对要搜索的原始内容进行索引构建一个索引库,索引过程包括:确定原始内容即要搜索的内容->采集文档->创建文档->分析文档->素引文档2. ...
- 关于git修改和查看用户名邮箱
git 查看/修改用户名.密码 查看用户名和邮箱地址: $ git config user.name $ git config user.email 修改用户名和邮箱地址: $ git config ...
- 生成字符Banner
生成字符Banner http://patorjk.com/software/taag __ _______/ |_ ____ ____ ____ / ___/\ __\/ _ \ / \ / _ \ ...
- 如何在Eclipse引入JUnit测试
1.在Eclipse中引入JUnitjar文件 2.右键Build Path>Configure Bulid Path 3.Libraries>Add Library 4.Junit> ...
- 【cl】eclipse基本设置(字体、配置JDK)
字体 找到上面的菜单“windows”打开Preferences 在弹出的设置窗口中找到“colors and fonts” 将 ...
- 有关java.util.ConcurrentModificationException
有关java.util.ConcurrentModificationException java doc对这个类的定义: This exception may be thrown by methods ...
- 2016.04.14,英语,《Vocabulary Builder》Unit 14
crypt/cryph, comes from the Greek word for 'hidden', encrypt, crypto- crypt : [krɪpt] n. 土窖, 地穴, (教堂 ...