SpringBoot第十四篇:在springboot中用redis实现消息队列
这篇文章主要讲述如何在springboot中用reids实现消息队列。
准备阶段
- 安装redis,可参考我的另一篇文章,5分钟带你入门Redis。
- java 1.8
- maven 3.0
- idea
环境依赖
创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
创建一个消息接收者
REcevier类,它是一个普通的类,需要注入到springboot中。
public class Receiver {private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);private CountDownLatch latch;@Autowiredpublic Receiver(CountDownLatch latch) {this.latch = latch;}public void receiveMessage(String message) {LOGGER.info("Received <" + message + ">");latch.countDown();}
}
注入消息接收者
@BeanReceiver receiver(CountDownLatch latch) {return new Receiver(latch);}@BeanCountDownLatch latch() {return new CountDownLatch(1);}@BeanStringRedisTemplate template(RedisConnectionFactory connectionFactory) {return new StringRedisTemplate(connectionFactory);}
注入消息监听容器
在spring data redis中,利用redis发送一条消息和接受一条消息,需要三样东西:
- 一个连接工厂
- 一个消息监听容器
- Redis template
上述1、3步已经完成,所以只需注入消息监听容器即可:
@BeanRedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter, new PatternTopic("chat"));return container;}@BeanMessageListenerAdapter listenerAdapter(Receiver receiver) {return new MessageListenerAdapter(receiver, "receiveMessage");}
测试
在springboot入口的main方法:
public static void main(String[] args) throws Exception{ApplicationContext ctx = SpringApplication.run(SpringbootRedisApplication.class, args);StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);CountDownLatch latch = ctx.getBean(CountDownLatch.class);LOGGER.info("Sending message...");template.convertAndSend("chat", "Hello from Redis!");latch.await();System.exit(0);}
先用redisTemplate发送一条消息,接收者接收到后,打印出来。启动springboot程序,控制台打印:
2017-04-20 17:25:15.536 INFO 39148 —- [ main] com.forezp.SpringbootRedisApplication : Sending message…
2017-04-20 17:25:15.544 INFO 39148 —- [ container-2] com.forezp.message.Receiver : 》Received
测试通过,接收者确实接收到了发送者的消息。
源码下载:
https://github.com/forezp/SpringBootLearning
参考资料
messaging-redis
SpringBoot第十四篇:在springboot中用redis实现消息队列相关推荐
- SpringBoot第二十四篇: springboot整合docker
这篇文篇介绍,怎么为 springboot程序构建一个docker镜像.docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的 ...
- SpringBoot第十五篇:Springboot整合RabbitMQ
这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息.我将构建一个springboot工程,通过RabbitTemplate去通过MessageListenerAdapter ...
- 企业级 SpringBoot 教程 (十四)在springboot中用redis实现消息队列
准备阶段 安装redis,可参考我的另一篇文章,5分钟带你入门Redis. java 1.8 maven 3.0 idea 环境依赖 创建一个新的springboot工程,在其pom文件,加入spri ...
- SpringBoot第十二篇:springboot集成apidoc
首先声明下,apidoc是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了springboot系列的完整性,所以标了个题. 一.apidoc简介 apidoc通过在你代码的注释来生 ...
- 《linux程序设计》--读书笔记--第十四章信号量、共享内存和消息队列
信号量:用于管理对资源的访问: 共享内存:用于在程序之间高效的共享数据: 消息队列:在程序之间传递数据的一种简单方法: 一.信号量 临界代码:需要确保只有一个进程或者一个执行线程可以进入这个临界代码并 ...
- Python之路【第十四篇】:AngularJS --暂无内容-待更新
Python之路[第十四篇]:AngularJS --暂无内容-待更新 转载于:https://www.cnblogs.com/weiman3389/p/6224181.html
- CCIE-LAB-第十四篇-PIM Sparst+IGMP
CCIE-LAB-第十四篇-PIM Sparst+IGMP 实际中,思科只会给你5个小时去做下面的全部配置 这个是CCIE-LAB的拓扑图 问题 翻译 FABD2正准备在其网络中启用PIM稀疏模式的中 ...
- CCNP-第十四篇-BGP综合实验
CCNP-第十四篇-BGP综合实验 大家好呀,学了那么长时间的BGP,又是一个巨大分水岭,那怎么能没综合实验呢? 答案以及个人录制视屏(数字那个视频因为录制软件的问题看不到命令行可以直接看后面的,不喜 ...
- CCNA-第十四篇-NAT-下+链路聚合(LACP)+DHCP
CCNA-第十四篇-NAT-下 这一篇是是针对一下华为设备的nat,然后讲讲链路聚合 下一篇来一个DHCP+一点点的SDN的介绍 **然后讲完SDN就基本上CCNA结束了哦** 华为的链路聚合叫Eth ...
最新文章
- scala 自带json_scala中复杂对象转JSON和解析的方法
- memcache windows64 位安装
- 腾讯游戏学院专家分析:Unity在移动设备的GPU内存机制
- Azure已成为微软业绩的顶梁柱
- 2017蓝桥杯省赛---java---C---1(外星日历)
- python遍历文件夹下所有文件名中含有指定_Python 遍历目录找出包含指定字符串的文件夹...
- ROS入门笔记(九):编写ROS的第一个程序hello world(重点)
- 碎裂效果尝试(clip-path篇)
- WinSock学习笔记(二)
- 电脑下边的任务栏不见了_Win7系统电脑任务栏经常自动消失不见的解决方法
- No.11软件工程的过程管理
- Qt QLineEdit QLabel 添加clicked事件
- 立秋后注意脚部保暖 试试量身定制合脚鞋
- ABAP报表添加点击跳转事务的方法
- 大数据是什么意思?大数据有哪些应用?
- 如何清除ug服务器注册码,UG许可证删除不掉的解决方法
- 苏宁搭台品牌唱戏,净水市场将变天
- 2D游戏和3D游戏的主要区别
- shell脚本根据指定日期 得到月初,月末,季初,季末,年初,年末,周开始,周结束,旬初,旬末日期,报表日期
- 2021最新大厂Java面试集合,顺利拿到offer