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

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

  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

  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

  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

  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

  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

RabbitMQ学习之spring配置文件rabbit标签的使用的更多相关文章

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

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

  2. Spring学习笔记--Spring配置文件和依赖注入

    Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...

  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配置文件beans标签报错问题解决

    因为有很多配置是复制过来的,附带的很多注释的格式会导致报错,所以可以要试试把注释去掉,只有配置文件的话可能就不会报错了.

  7. Spring 源码(4)在Spring配置文件中自定义标签如何实现?

    Spring 配置文件自定义标签的前置条件 在上一篇文章https://www.cnblogs.com/redwinter/p/16165274.html Spring BeanFactory的创建过 ...

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

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

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

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

随机推荐

  1. Google Shell Style Guide

    转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...

  2. Python中字符串操作函数string.split('str1')和string.join(ls)

    Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换, 使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表 def split(self, * ...

  3. 1.sts的下载安装

    sts的官方下载如下: http://spring.io/tools3/sts/all/ 将下载后的压缩文件解压,在解压后的sts-bundle下的sts-3.9.1RELEASE目录中STS.exe ...

  4. 【ACM】poj_2080_Calendar_201307311043

    CalendarTime Limit: 1000MS  Memory Limit: 30000K Total Submissions: 9787  Accepted: 3677 Description ...

  5. [bzoj1369][Baltic2003]Gem_树形dp_结论题

    Gem bzoj-1369 Baltic-2003 题目大意:给你一棵树,让你往节点上添自然数,使得任意相邻节点的数不同且使得权值最小. 注释:n为结点个数,$1\le n\le 10^3$. 想法: ...

  6. Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容

    Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容 当Git无法自动合并分支时,就必须首先解 ...

  7. LNMP一键安装包 V1.1 公布

    LNMP一键安装包 是一个用Linux Shell编写的能够为CentOS/RadHat.Debian/Ubuntu VPS(VDS)或独立主机安装LNMP(Nginx.MySQL/MariaDB.P ...

  8. 2.【SELinux学习笔记】概念

    1.强制类型的安全上下文     在SELinux中,訪问控制属性叫做安全上上下文.不管主体还是客体都有与之关联的安全上下文.通常安全上下文是由三部分组成:用户:角色:类型. 如: $id -Z  j ...

  9. iOS GCD使用指南

    Grand Central Dispatch(GCD)是异步运行任务的技术之中的一个. 一般将应用程序中记述的线程管理用的代码在系统级中实现.开发人员仅仅须要定义想运行的任务并追加到适当的Dispat ...

  10. nginx源代码分析--进程间通信机制 &amp; 同步机制

    Nginx源代码分析-进程间通信机制 从nginx的进程模型能够知道.master进程和worker进程须要通信,nginx中通信的方式有套接字.共享内存.信号.对于master进程,从外部接受信号, ...