spring-rabbitmq的源码到http://github.com/momania/spring-rabbitmq下载,并可以下载实例代码。由于我使用的rabbitmq版本是3.0.4,部分代码做了调整。

具体实例如下(创建自动删除非持久队列):

1.资源配置application.properties

  1. #============== rabbitmq config ====================
  2. rabbit.hosts=192.168.36.102
  3. rabbit.username=admin
  4. rabbit.password=admin
  5. rabbit.virtualHost=/
  6. rabbit.exchange=spring-queue-async
  7. rabbit.queue=spring-queue-async
  8. rabbit.routingKey=spring-queue-async

2..发送端配置applicationContext-rabbitmq-async-send.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <context:property-placeholder location="classpath:application.properties"/>
  9. <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
  10. <property name="connectionFactory">
  11. <bean class="com.rabbitmq.client.ConnectionFactory">
  12. <property name="username" value="${rabbit.username}"/>
  13. <property name="password" value="${rabbit.password}"/>
  14. <property name="virtualHost" value="${rabbit.virtualHost}"/>
  15. </bean>
  16. </property>
  17. <property name="hosts" value="${rabbit.hosts}"/>
  18. </bean>
  19. <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
  20. <property name="connectionFactory" ref="rabbitConnectionFactory"/>
  21. </bean>
  22. <bean id="rabbitTemplate" class="com.rabbitmq.spring.template.ASyncRabbitTemplate">
  23. <property name="channelFactory" ref="rabbitChannelFactory"/>
  24. <property name="exchange" value="${rabbit.exchange}"/>
  25. <property name="routingKey" value="${rabbit.routingKey}"/>
  26. <!--optional-->
  27. <property name="exchangeType" value="TOPIC"/>
  28. <!--         mandatory是否强制发送       -->
  29. <property name="mandatory" value="false"/>
  30. <!--         immediate是否立即发送   -->
  31. <property name="immediate" value="false"/>
  32. </bean>
  33. </beans>

3.接收端配置applicationContext-rabbitmq-async-receive.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <context:property-placeholder location="classpath:application.properties"/>
  9. <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
  10. <property name="connectionFactory">
  11. <bean class="com.rabbitmq.client.ConnectionFactory">
  12. <property name="username" value="${rabbit.username}"/>
  13. <property name="password" value="${rabbit.password}"/>
  14. <property name="virtualHost" value="${rabbit.virtualHost}"/>
  15. </bean>
  16. </property>
  17. <property name="hosts" value="${rabbit.hosts}"/>
  18. </bean>
  19. <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
  20. <property name="connectionFactory" ref="rabbitConnectionFactory"/>
  21. </bean>
  22. <bean id="receiveMsgHandler" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async.ReceiveMsgHandler"/>
  23. <bean id="quotingParamtersTopicAdapter" class="com.rabbitmq.spring.listener.RabbitMessageListenerAdapter">
  24. <property name="channelFactory" ref="rabbitChannelFactory"/>
  25. <property name="delegate" ref="receiveMsgHandler"/>
  26. <property name="listenerMethod" value="handleMessage"/>
  27. <property name="exchange" value="${rabbit.exchange}"/>
  28. <!--optional-->
  29. <property name="exchangeType" value="TOPIC"/>
  30. <property name="routingKey" value="${rabbit.routingKey}"/>
  31. <property name="queueName" value="${rabbit.queue}"/>
  32. <property name="poolsize" value="5"/>
  33. </bean>
  34. </beans>

4.消息处理服务ReceiveMsgHandler.Java

  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
  2. public class ReceiveMsgHandler {
  3. public void handleMessage(String text) {
  4. System.out.println("Received: " + text);
  5. }
  6. }

5.发送端启动代码Send.java

  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.rabbitmq.spring.template.ASyncRabbitTemplate;
  5. public class Send {
  6. public static void main(String[] args) throws InterruptedException {
  7. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-async-send.xml");
  8. ASyncRabbitTemplate amqpTemplate = context.getBean(ASyncRabbitTemplate.class);
  9. for(int i=0;i<10000;i++){
  10. amqpTemplate.send("test spring async=>"+i);
  11. Thread.sleep(100);
  12. }
  13. }
  14. }

6.接收端启动代码Send.java

  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class Receive {
  4. public static void main(String[] args) {
  5. new ClassPathXmlApplicationContext("applicationContext-rabbitmq-async-receive.xml");
  6. }
  7. }

先启动接收端,再启动发送端。接收到消息如下:

  1. Received: test spring async=>0
  2. Received: test spring async=>1
  3. Received: test spring async=>2
  4. Received: test spring async=>3
  5. Received: test spring async=>4
  6. Received: test spring async=>5
  7. Received: test spring async=>6
  8. Received: test spring async=>7
  9. ......

实例代码:http://download.csdn.net/detail/tianwei7518/8135637

RabbitMQ学习之基于spring-rabbitmq的消息异步发送的更多相关文章

  1. RabbitMQ学习笔记五:RabbitMQ之优先级消息队列

    RabbitMQ优先级队列注意点: 1.只有当消费者不足,不能及时进行消费的情况下,优先级队列才会生效 2.RabbitMQ3.5以后才支持优先级队列 代码在博客:RabbitMQ学习笔记三:Java ...

  2. RabbitMQ学习以及与Spring的集成(三)

    本文介绍RabbitMQ与Spring的简单集成以及消息的发送和接收. 在RabbitMQ的Spring配置文件中,首先需要增加命名空间. xmlns:rabbit="http://www. ...

  3. RabbitMQ学习之基于spring-rabbitmq的RPC远程调用

    http://blog.csdn.net/zhu_tianwei/article/details/40920985 spring-rabbitmq中实现远程接口调用,主要在com.rabbitmq.s ...

  4. RabbitMQ学习以及与Spring的集成(二)

    本文介绍RabbitMQ的一些基本概念. RabbitMQ服务可以安装在独立服务器上,通过配置的账户和ip访问使用.也就是说,RabbitMQ和使用它的应用可以部署在不同的服务器上.RabbitMQ的 ...

  5. RabbitMQ学习笔记之五种模式及消息确认机制

    本文详细介绍简单模式Simple.工作模式Work.发布订阅模式Publish/Subscribe.Topic.Routing. Maven依赖引用 <dependencies> < ...

  6. RabbitMQ学习系列一安装RabbitMQ服务

    RabbitMQ学习系列一:windows下安装RabbitMQ服务 http://www.80iter.com/blog/1437026462550244 Rabbit MQ 是建立在强大的Erla ...

  7. 如何在优雅地Spring 中实现消息的发送和消费

    本文将对rocktmq-spring-boot的设计实现做一个简单的介绍,读者可以通过本文了解将RocketMQ Client端集成为spring-boot-starter框架的开发细节,然后通过一个 ...

  8. RabbitMQ学习以及与Spring的集成(一)

    本文记录RabbitMQ服务的搭建过程. 想要使用RabbitMQ消息中间件服务.首先要安装RabbitMQ,可以在:https://www.rabbitmq.com/download.html根据安 ...

  9. RabbitMQ学习笔记六:RabbitMQ之消息确认

    使用消息队列,必须要考虑的问题就是生产者消息发送失败和消费者消息处理失败,这两种情况怎么处理. 生产者发送消息,成功,则确认消息发送成功;失败,则返回消息发送失败信息,再做处理. 消费者处理消息,成功 ...

随机推荐

  1. 记一次获得 3 倍性能的 go 程序优化实践,及 on-cpu / off-cpu 火焰图的使用

    转自:https://mp.weixin.qq.com/s/9IKaXeWTiiQTFlvZzxgsEA 记一次获得 3 倍性能的 go 程序优化实践,及 on-cpu / off-cpu 火焰图的使 ...

  2. 【VIP视频网站项目一】搭建视频网站的前台页面(导航栏+轮播图+电影列表+底部友情链接)

    首先来直接看一下最终的效果吧: 项目地址:https://github.com/xiugangzhang/vip.github.io 在线预览地址:https://xiugangzhang.githu ...

  3. Silverlight之我见——制作星星闪烁动画

    圣诞节来了,无聊,做点东西纪念一下. 原理很简单,生成1000个圆,从随机数来布置它们的位置,通过动画来处理它们的透明度,动画时长也是随机生成. 1.创建图形数组并设置背景透明,渐变笔触,大小等,而后 ...

  4. Git 基础教程 之 删除文件

    ① 手动或命令 rm删除工作区的问价:       git checkout -- readme.txt 可恢复       checkout 实际上是用版本库里的替换工作区的版本 ② 删除了工作区文 ...

  5. Codeforces 902C/901A - Hashing Trees

    传送门:http://codeforces.com/contest/902/problem/C 本题是一个关于“树”的问题. 将一棵高度为h的有根树表示为数列{ai|i=0,1,2,...,h},其中 ...

  6. 【Codeforces 350B】Resort

    [链接] 我是链接,点我呀:) [题意] [题解] 我们可以把原图的边都反向一下. 然后以每个休息点作为起点,进行dfs. 每次在扩展节点y的时候,要求这个点y必须只有一个出度,然后就能走多远就走多远 ...

  7. UDP bind() IP和prot

    http://blog.csdn.net/qq_28171461/article/details/69523604

  8. [Project]微信项目感悟

    一定要先考虑好可复用部分,可以复制粘贴的地方 一定要先想好了在动 前台不同插件之间的兼容性问题可能是dom加载顺序的问题,有的代码可能要卸载其中一个插件的某个事件里

  9. 状态压缩dp poj 3254 hdu5045

    近来感觉状态压缩dp的强大性(灵活利用了二进制运算非常关键). . . 于是做了俩提来看看..毕竟队友是专业的dp.我仅仅是管中窥豹下而已.. 日后有机会再与之玩耍玩耍...ps:假设上天再给我一次机 ...

  10. Sublime Text 2 SFTP UnicodeDecodeError错误!

    右键-->SFTP/FTP ->Sync Remote To Local {作者:半条虫(466814195)} 提示下面错误 An unexpected error occurred, ...