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