RabbitMQ学习总结(7)——Spring整合RabbitMQ实例

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1.RabbitMQ简介

RabbitMQ是流行的开源消息队列系统,用erlang语言开发。RabbitMQ是AMQP(高级消息队列协议)的标准实现。 
官网:http://www.rabbitmq.com/

2.Spring集成RabbitMQ

2.1 maven配置

//pom.xml
<dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>1.4.5.RELEASE</version></dependency>

2.2 rabbmitmq配置文件

//rabbitmq-config.properties
mq.host=127.0.0.1
mq.username=test
mq.password=123456
mq.port=5672
mq.vhost=testmq

2.3 Spring配置

//application-mq.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd" ><description>rabbitmq 连接服务配置</description><!-- 连接配置 --><rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" port="${mq.port}"  virtual-host="${mq.vhost}"/><rabbit:admin connection-factory="connectionFactory"/><!-- spring template声明--><rabbit:template exchange="amqpExchange" id="amqpTemplate"  connection-factory="connectionFactory"  message-converter="jsonMessageConverter" /><!-- 消息对象json转换类 --><bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />  
</beans>

3. 在Spring中使用RabbitMQ

3.1 申明一个消息队列Queue

//application-mq.xml
<rabbit:queue id="test_queue_key" name="test_queue_key" durable="true" auto-delete="false" exclusive="false" />

说明: 

durable:是否持久化

exclusive: 仅创建者可以使用的私有队列,断开后自动删除

auto_delete: 当所有消费客户端连接断开后,是否自动删除队列

3.2 交换机定义

//application-mq.xml
<rabbit:direct-exchange name="test-mq-exchange" durable="true" auto-delete="false" id="test-mq-exchange"><rabbit:bindings><rabbit:binding queue="test_queue_key" key="test_queue_key"/></rabbit:bindings>
</rabbit:direct-exchange>

说明: 

rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。 

rabbit:binding:设置消息queue匹配的key

3.3 发送消息Producer

//MQProducer.java
public interface MQProducer {/*** 发送消息到指定队列* @param queueKey* @param object*/public void sendDataToQueue(String queueKey, Object object);
}


@Service
public class MQProducerImpl implements MQProducer {@Autowiredprivate AmqpTemplate amqpTemplate;private final static Logger LOGGER = Logger.getLogger(MQProducerImpl.class);/* (non-Javadoc)* @see com.stnts.tita.rm.api.mq.MQProducer#sendDataToQueue(java.lang.String, java.lang.Object)*/@Overridepublic void sendDataToQueue(String queueKey, Object object) {try {amqpTemplate.convertAndSend(queueKey, object);} catch (Exception e) {LOGGER.error(e);}}
}

说明: 

convertAndSend:将Java对象转换为消息发送到匹配Key的交换机中Exchange,由于配置了JSON转换,这里是将Java对象转换成JSON字符串的形式。原文:Convert a Java object to an Amqp Message and send it to a default exchange with a specific routing key.

3.4 异步接收消息Consumer

定义监听器

//QueueListenter.java
@Component
public class QueueListenter implements MessageListener {@Overridepublic void onMessage(Message msg) {try{System.out.print(msg.toString());}catch(Exception e){e.printStackTrace();}}}

监听配置

//application-mq.xml
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto"><rabbit:listener queues="test_queue" ref="queueListenter"/>
</rabbit:listener-container>

说明: 

queues:监听的队列,多个的话用逗号(,)分隔 

ref:监听器

3.5 JUnit测试

//TestQueue.java
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/ApplicationContext/ApplicationContext-mq.xml"})public class TestQueue{@AutowiredMQProducer mqProducer;final String queue_key = "test_queue";@Testpublic void send(){Map<String,Object> msg = new HashMap()<>;msg.put("data","hello,rabbmitmq!");mqProducer.sendDataToQueue(query_key,msg);}
}

运行测试程序,Run with JUnit,会发送一条消息到test_queue,监听器监听到消息后,打印出消息。

至此,已经完成了Spring和RabbmitMQ集成,配置,和使用。

转载于:https://my.oschina.net/zhanghaiyang/blog/597406

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274939.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

UIScrollView实现不全屏分页的小技巧

浏览一系列图片时&#xff0c;我们会经常使用UIScrollView来进行实现&#xff0c;因为其自带的pagingEnabled属性&#xff0c;可以非常方便的自动支持手势左右轻扫切换切换页面。pagingEnabled不过最近做的一个产品&#xff0c;设计的效果有所不同&#xff0c;希望每一页图片的…

谈谈对java中分层的理解_让我们谈谈网页设计中的卡片设计

谈谈对java中分层的理解“I want a card”, this is the first demand point that the customer said in the last issue when talking to me about demand. There is no doubt that the card type is excellent for both PC and mobile phones. From online shopping malls to…

1-jdk的安装与配置

1- Jvm、jdk、jre之间的关系 JVM&#xff1a;Java虚拟机&#xff0c;保证java程序跨平台。&#xff08;Java Virtual Machine&#xff09;JRE&#xff1a; Java运行环境&#xff0c;包含JVM和核心类库。如果只是想运行java程序&#xff0c;只要安装JRE即可。&#xff08;Java R…

来自未来,2022 年的前端人都在做什么?

大家好&#xff0c;我是若川。持续组织了6个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。来自上帝视角的总览…

模拟

状态压缩 uva 12368 - Candles 2011 - Dhaka 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<string> 5 #include<algorithm> 6 #include<math.h> 7 #include<map> 8 #include<queue> 9 using…

qt ui指针和本类对象_您需要了解的有关UI设计的形状和对象的所有信息

qt ui指针和本类对象重点 (Top highlight)第1部分 (Part 1) So you’re thinking about becoming a UX/UI designer, but are afraid to start? Don’t worry. It’s easier than you think. You only need a solid foundation and a lot of dedication. I can’t help you wi…

移动端h5页面复制粘贴(兼容到ios9安卓4.0.0)

项目使用jquery框架纯h5复制粘贴解决了iscroll4阻止所有默认事件&#xff0c;长按无法弹出复制菜单项目需要写一个上拉加载页面&#xff0c;页面中的物流信息需要可复制粘贴&#xff0c;同时需要兼容到ios9和安卓4.0.0, 经过大量搜索&#xff0c;最终使用clipboard。代码如下 &…

2021 大前端技术回顾及未来展望

大家好&#xff0c;我是若川。持续组织了6个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试系列2021 …

最近想读的书想做的事

越来越多了&#xff0c;但是总是还没开始&#xff0c;一天拿出一小时&#xff0c;周末拿出一上午。加油加油 2012/3/27 C标准库 C专家编程Expert C Programming Deep C Secrets CodeCraft编程匠艺&#xff1a;编写卓越的代码 代码之美 重看 编程珠玑 编程卓越之道 卷1 卷2 代码…

skysat重访周期_重访小恶梦

skysat重访周期You awaken with a start, the nightmare still fogging your mind with terror. Rain falls through cracks in the ceiling above you. The room is sparse, metallic, desolate. Searching the pockets of your yellow raincoat, you find only a cigarette l…

Python这些位运算的妙用,绝对让你大开眼界

位运算的性能大家想必是清楚的&#xff0c;效率绝对高。相信爱好源码的同学&#xff0c;在学习阅读源码的过程中会发现不少源码使用了位运算。但是为啥在实际编程过程中应用少呢&#xff1f;想必最大的原因&#xff0c;是较为难懂。不过&#xff0c;在面试的过程中&#xff0c;…

记一次 Vue2 迁移 Vue3 的实践总结

大家好&#xff0c;我是若川。持续组织了6个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试系列一、V…

改错3-38

#include<iostream.h>class time{private:int hour,minute,second;public:void settime(int h,int m,int s) { hour(h>0&&h<24)?h:0; minute(m>0&&m<60)?m:0; second(s>0&&s<60)?s:0; }void sh…

魔兽怀旧网站模块下载_一个人的网站重新设计和怀旧

魔兽怀旧网站模块下载Despite how I look, I’m the kind kind of person that loves to play old video games. (Full disclosure: I look exactly like the kind of person that loves to play old video games).尽管我长得很帅&#xff0c;但我还是一个喜欢玩旧视频游戏的人…

华为架构师谈如何理解运用模块与微服务

模块化还是微服务&#xff1f; 我们的业务由一个大型应用转向微服务的时候&#xff0c;除了很好展示漂亮的PPT&#xff0c;提升KPI之外&#xff0c;实际操作时将整个业务切成微型服务似乎也不费吹灰之力。但这种方法真的是我们的最佳选择吗&#xff1f;确实&#xff0c;维护凌乱…

Node.js 可以和 Web 实现 HTTP 请求的跨平台兼容了!

大家好&#xff0c;我是若川。持续组织了6个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试系列大家好…

zeplin加载 不出图片_为什么Zeplin不能解决您的所有问题

zeplin加载 不出图片Design handover involves communicating the visual styles and behaviours of your design so they can be translated into code.设计移交涉及传达设计的视觉样式和行为&#xff0c;以便可以将它们转换为代码。 Back in the Dark Ages of digital desig…

POJ 基础数学

数学 组合数学 POJ3252,poj1850,poj1019,poj1942 数论 poj2635, poj3292,poj1845,poj2115 计算方法&#xff08;二分&#xff09; poj3273,poj3258,poj1905,poj3122 组合数学 poj 3252 题意&#xff1a;如果一个数是round number&#xff0c;则它的二进制表示中&#xff…

使用uwsgi和gunicorn部署Django项目

https://uwsgi-docs.readthedocs.io/en/latest/Management.html https://uwsgi-docs.readthedocs.io/en/latest/Management.html 先了解下相关杀进程命令 ps -ef|grep uwsgi|grep -v grep|awk {print $2}|xargs kill -9//查看uwsgi相关接口 ps -ef|grep uwsgi #查看相关端口 ne…

推荐2022前端必看的新书 《Vue.js设计与实现》

大家好&#xff0c;我是若川。持续组织了6个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。历史面试系列这本新…