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

最新文章

  1. 介绍一种基于gulp对seajs的模块做合并压缩的方式
  2. 一个简单的MVC实例及故障排除
  3. zepto源码--核心方法2(class相关)--学习笔记
  4. 针对android方法数64k的限制,square做出的努力。精简protobuf
  5. algorithm之不变序列操作
  6. Java到底是不是一种纯面向对象语言?
  7. ecshop后台根据条件查询后不填充table 返回的json数据,content为空?
  8. 【转】Android 菜单(OptionMenu)大全 建立你自己的菜单--不错
  9. Qt学习之路(58): 进程间交互(QProcess.readAllStandardOutput可以读取控制台的输出)
  10. 实现各种 CSS3 文本动画效果
  11. 修改android应用包名
  12. 高可用实现KeepAlived原理简介
  13. ajax 提交Dictionary
  14. XamarinAndroid组件教程RecylerView动画组件使用动画(3)
  15. python gevent自动挡的协程切换。
  16. 2018.12.30 bzoj3028: 食物(生成函数)
  17. jquery判断日期是不是为空,是否大于前面的日期
  18. BZOJ 1196: [HNOI2006]公路修建问题 Kruskal/二分
  19. 关于Cocos2d-x节点和精灵节点的坐标、位置以及大小的设置
  20. php版本管理工具composer安装及使用

热门文章

  1. 在 Linux 下判断系统当前是否开启了超线程
  2. 一个vue模拟键盘的组件
  3. 亚马逊免费服务器搭建Discuz!论坛过程(一)
  4. opencv学习HighGUI图形用户界面初步【1】
  5. 【[Offer收割]编程练习赛15 C】过河问题
  6. docker 容器操作( 以 tomcat 为例 )
  7. rar x 解压rar文件,提示permission denied
  8. 单片机小白学步系列(十四) 点亮第一个LED的程序分析
  9. MySQL数据库管理(二)单机环境下MySQL Cluster的安装
  10. log4net写日志的时间附带时区信息