RabbitMQ学习之基于spring-rabbitmq的消息异步发送
spring-rabbitmq的源码到http://github.com/momania/spring-rabbitmq下载,并可以下载实例代码。由于我使用的rabbitmq版本是3.0.4,部分代码做了调整。
具体实例如下(创建自动删除非持久队列):
1.资源配置application.properties
- #============== rabbitmq config ====================
- rabbit.hosts=192.168.36.102
- rabbit.username=admin
- rabbit.password=admin
- rabbit.virtualHost=/
- rabbit.exchange=spring-queue-async
- rabbit.queue=spring-queue-async
- rabbit.routingKey=spring-queue-async
2..发送端配置applicationContext-rabbitmq-async-send.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:property-placeholder location="classpath:application.properties"/>
- <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
- <property name="connectionFactory">
- <bean class="com.rabbitmq.client.ConnectionFactory">
- <property name="username" value="${rabbit.username}"/>
- <property name="password" value="${rabbit.password}"/>
- <property name="virtualHost" value="${rabbit.virtualHost}"/>
- </bean>
- </property>
- <property name="hosts" value="${rabbit.hosts}"/>
- </bean>
- <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
- <property name="connectionFactory" ref="rabbitConnectionFactory"/>
- </bean>
- <bean id="rabbitTemplate" class="com.rabbitmq.spring.template.ASyncRabbitTemplate">
- <property name="channelFactory" ref="rabbitChannelFactory"/>
- <property name="exchange" value="${rabbit.exchange}"/>
- <property name="routingKey" value="${rabbit.routingKey}"/>
- <!--optional-->
- <property name="exchangeType" value="TOPIC"/>
- <!-- mandatory是否强制发送 -->
- <property name="mandatory" value="false"/>
- <!-- immediate是否立即发送 -->
- <property name="immediate" value="false"/>
- </bean>
- </beans>
3.接收端配置applicationContext-rabbitmq-async-receive.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:property-placeholder location="classpath:application.properties"/>
- <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
- <property name="connectionFactory">
- <bean class="com.rabbitmq.client.ConnectionFactory">
- <property name="username" value="${rabbit.username}"/>
- <property name="password" value="${rabbit.password}"/>
- <property name="virtualHost" value="${rabbit.virtualHost}"/>
- </bean>
- </property>
- <property name="hosts" value="${rabbit.hosts}"/>
- </bean>
- <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
- <property name="connectionFactory" ref="rabbitConnectionFactory"/>
- </bean>
- <bean id="receiveMsgHandler" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async.ReceiveMsgHandler"/>
- <bean id="quotingParamtersTopicAdapter" class="com.rabbitmq.spring.listener.RabbitMessageListenerAdapter">
- <property name="channelFactory" ref="rabbitChannelFactory"/>
- <property name="delegate" ref="receiveMsgHandler"/>
- <property name="listenerMethod" value="handleMessage"/>
- <property name="exchange" value="${rabbit.exchange}"/>
- <!--optional-->
- <property name="exchangeType" value="TOPIC"/>
- <property name="routingKey" value="${rabbit.routingKey}"/>
- <property name="queueName" value="${rabbit.queue}"/>
- <property name="poolsize" value="5"/>
- </bean>
- </beans>
4.消息处理服务ReceiveMsgHandler.Java
- package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
- public class ReceiveMsgHandler {
- public void handleMessage(String text) {
- System.out.println("Received: " + text);
- }
- }
5.发送端启动代码Send.java
- package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.rabbitmq.spring.template.ASyncRabbitTemplate;
- public class Send {
- public static void main(String[] args) throws InterruptedException {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-async-send.xml");
- ASyncRabbitTemplate amqpTemplate = context.getBean(ASyncRabbitTemplate.class);
- for(int i=0;i<10000;i++){
- amqpTemplate.send("test spring async=>"+i);
- Thread.sleep(100);
- }
- }
- }
6.接收端启动代码Send.java
- package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Receive {
- public static void main(String[] args) {
- new ClassPathXmlApplicationContext("applicationContext-rabbitmq-async-receive.xml");
- }
- }
先启动接收端,再启动发送端。接收到消息如下:
- Received: test spring async=>0
- Received: test spring async=>1
- Received: test spring async=>2
- Received: test spring async=>3
- Received: test spring async=>4
- Received: test spring async=>5
- Received: test spring async=>6
- Received: test spring async=>7
- ......
实例代码:http://download.csdn.net/detail/tianwei7518/8135637
RabbitMQ学习之基于spring-rabbitmq的消息异步发送的更多相关文章
- RabbitMQ学习笔记五:RabbitMQ之优先级消息队列
RabbitMQ优先级队列注意点: 1.只有当消费者不足,不能及时进行消费的情况下,优先级队列才会生效 2.RabbitMQ3.5以后才支持优先级队列 代码在博客:RabbitMQ学习笔记三:Java ...
- RabbitMQ学习以及与Spring的集成(三)
本文介绍RabbitMQ与Spring的简单集成以及消息的发送和接收. 在RabbitMQ的Spring配置文件中,首先需要增加命名空间. xmlns:rabbit="http://www. ...
- RabbitMQ学习之基于spring-rabbitmq的RPC远程调用
http://blog.csdn.net/zhu_tianwei/article/details/40920985 spring-rabbitmq中实现远程接口调用,主要在com.rabbitmq.s ...
- RabbitMQ学习以及与Spring的集成(二)
本文介绍RabbitMQ的一些基本概念. RabbitMQ服务可以安装在独立服务器上,通过配置的账户和ip访问使用.也就是说,RabbitMQ和使用它的应用可以部署在不同的服务器上.RabbitMQ的 ...
- RabbitMQ学习笔记之五种模式及消息确认机制
本文详细介绍简单模式Simple.工作模式Work.发布订阅模式Publish/Subscribe.Topic.Routing. Maven依赖引用 <dependencies> < ...
- RabbitMQ学习系列一安装RabbitMQ服务
RabbitMQ学习系列一:windows下安装RabbitMQ服务 http://www.80iter.com/blog/1437026462550244 Rabbit MQ 是建立在强大的Erla ...
- 如何在优雅地Spring 中实现消息的发送和消费
本文将对rocktmq-spring-boot的设计实现做一个简单的介绍,读者可以通过本文了解将RocketMQ Client端集成为spring-boot-starter框架的开发细节,然后通过一个 ...
- RabbitMQ学习以及与Spring的集成(一)
本文记录RabbitMQ服务的搭建过程. 想要使用RabbitMQ消息中间件服务.首先要安装RabbitMQ,可以在:https://www.rabbitmq.com/download.html根据安 ...
- RabbitMQ学习笔记六:RabbitMQ之消息确认
使用消息队列,必须要考虑的问题就是生产者消息发送失败和消费者消息处理失败,这两种情况怎么处理. 生产者发送消息,成功,则确认消息发送成功;失败,则返回消息发送失败信息,再做处理. 消费者处理消息,成功 ...
随机推荐
- C++ 资源大全中文版
标准库 C++标准库,包括了STL容器,算法和函数等. C++ Standard Library:是一系列类和函数的集合,使用核心语言编写,也是C++ISO自身标准的一部分. Standard Tem ...
- 洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk【贪心+背包】
由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的.此 ...
- CSS font-style中italic和Oblique有何区别 标签: css字体 2017-01-05 14:42 60人阅读 评论
*要搞清楚这个问题,首先要明白字体是怎么回事.一种字体有粗体.斜体.下划线.删除线等诸多属性. 但是并不是所有字体都做了这些,一些不常用的字体,或许就只有个正常体,如果你用Italic,就没有效果了~ ...
- EasyUI Messager 消息框点击右上角x无法执行回掉函数解决
我先声明下,我是改动源码,网上的其他方法我试过了,不行.我是写java后台的,前端也是小白,可能讲的也也不是很清楚. 首先我想到的是在网上找到解决方法,我找到一个方法说在jquery.easyui.m ...
- String去除重复字符两个方法
package cn.aresoft; import java.util.ArrayList;import java.util.List; public class TestBasic { publi ...
- Spring学习总结(2)——Spring的常用注解
本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). ? 1 <context:compon ...
- 解读grub.conf文件
我们将来看看grub.conf文件内语句,(注:...)内的东西是我们的解读内容. # grub.conf generated by anaconda # # Note that you do not ...
- linux 7.2 下安装maven
由于现有项目采用maven打包所以需要安装maven 1.创建目录 mkdir /maven cd /maven 2.下载解压maven,这里选择maven版本为3.5.3 wget http://m ...
- ABCDE
ABCDE A-Artificial intelligence 人工智能 B-Block chain 区块链 C-Cloud 云 D-Big Data 大数据 E-Ecology 互联网生态是以互联网 ...
- sqlite3树形结构遍历效率对照測试
sqlite3树形结构遍历效率对照測试 一.缘起 项目数据结构:本人从事安防行业,视频监控领域.项目中会遇到监控点位的组织机构划分.暂时划分的巡逻点位等.这些相机点位.连同组织机构,它们在逻辑关系上构 ...