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,一经查实,立即删除!

相关文章

谈谈对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余篇源码文章。来自上帝视角的总览…

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…

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

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

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…

记一次 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;但我还是一个喜欢玩旧视频游戏的人…

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…

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

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

汉堡菜单_汉堡菜单-可访问性和用户体验设计原则的挑战?

汉堡菜单重点 (Top highlight)I was recently designing a hamburger menu for a client and before I knew it, I had embarked on this journey where I was reading article after article about the accessibility issues which accompany a hamburger icon. Turns out, th…

Server2012R2 ADFS3.0 The same client browser session has made '6' requests in the last '13'seconds

本问题是在windows server2012R2系统ADFS3.0环境下遇到的&#xff0c;CRM2013部署ADFS后运行一段时间(大概有一两个月)后在IE浏览器中访问登陆界面点击登陆后就报以下错误 “Microsoft.IdentityServer.Web.InvalidRequestException: MSIS7042: The same client browser session…

又一个基于 Esbuild 的神器!esno

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

c# ui 滚动 分页_UI备忘单:分页,无限滚动和“加载更多”按钮

c# ui 滚动 分页重点 (Top highlight)When you have a lot of content, you have to rely on one of these three patterns to load it. So, which is best? What will your users like? What do most platforms use? These are the questions we will explore today.当内容…

少年,看你异于常人,有空花2小时来参加有3000人的源码共读嘛~

大家好&#xff0c;我是若川。按照从易到难的顺序&#xff0c;前面几期&#xff08;比如&#xff1a;validate-npm-package-name、axios工具函数&#xff09;很多都只需要花2-3小时就能看完&#xff0c;并写好笔记。但收获确实很大。开阔视野、查漏补缺、升职加薪。已经有400笔…

16位调色板和32位调色板_使调色板可访问

16位调色板和32位调色板Accessibility has always been a tough sell. Admittedly, less so than in the ‘nineties, when no prospective client was interested. But even today — more enlightened times — the majority of companies I encounter still prefer to make …

从零开始发布自己的NPM包

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