在springboot中用redis实现消息队列
环境依赖
创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
创建一个消息接收者
REcevier类,它是一个普通的类,需要注入到springboot中。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class Receiver { private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class); private CountDownLatch latch; @Autowired public Receiver(CountDownLatch latch) { this.latch = latch; } public void receiveMessage(String message) { LOGGER.info("Received <" + message + ">"); latch.countDown(); }} |
注入消息接收者
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Bean Receiver receiver(CountDownLatch latch) { return new Receiver(latch); } @Bean CountDownLatch latch() { return new CountDownLatch(1); } @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { return new StringRedisTemplate(connectionFactory); } |
注入消息监听容器
在spring data redis中,利用redis发送一条消息和接受一条消息,需要三样东西:
- 一个连接工厂
- 一个消息监听容器
- Redis template
上述1、3步已经完成,所以只需注入消息监听容器即可:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter, new PatternTopic("chat")); return container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); } |
测试
在springboot入口的main方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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程序,控制台打印:
|
1
2
|
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 |

在springboot中用redis实现消息队列的更多相关文章
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot14-redis-mq/ 本文出自方志朋的博客 这 ...
- Spring Boot教程(一)在springboot中用redis实现消息队列
环境依赖 创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖: <dependency> <groupId&g ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- Redis 做消息队列
一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现.定义: 生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...
- Redis作为消息队列服务场景应用案例
NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例 一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更 ...
- redis resque消息队列
Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- 【Redis】php+redis实现消息队列
在项目中使用消息队列一般是有如下几个原因: 把瞬间服务器的请求处理换成异步处理,缓解服务器的压力 实现数据顺序排列获取 redis实现消息队列步骤如下: 1).redis函数rpush,lpop 2) ...
- Lumen开发:结合Redis实现消息队列(1)
1.简介 Lumen队列服务为各种不同的后台队列提供了统一的API.队列允许你推迟耗时任务(例如发送邮件)的执行,从而大幅提高web请求速度. 1.1 配置 .env文件的QUEUE_DRIVER选项 ...
随机推荐
- _itemmod_gem_remove
该表可配置以一定代价移除宝石,移除后获得该宝石 `entry`宝石ID `reqId` 需求ID `chance`几率 `comond` 备注
- 【一】jquery之subline编辑器插件安装
1.地址下载:https://pan.baidu.com/share/link?shareid=552312&uk=151954025 2.打开Sublime, 选择 Prefreences ...
- 再谈java编码
一篇好文:从原理上搞懂编码——究竟什么是编码?什么是解码?什么是字节流? encode,即把字符按照指定的<编码gbk utf-8等>编码成该<编码>所表示的字节 decode ...
- log4net配置使用
1.配置文件 app.config <?xml version="1.0" encoding="utf-8" ?> <configuratio ...
- 【C#】调用2.0踩过的坑
1.初始化[DllImport(“libarcsoft_face_engine.dll”, EntryPoint = “ASFInitEngine”, CallingConvention = Call ...
- angular7 + d3 显示svg
汇总一些之前没有注意到的问题 总体思路: app只是显示svg为主,接收后端推送的数据改变,显示变化后的svg. 因此,只用d3的数据绑定更新组件里<svg></svg>节点. ...
- Linux学习3-yum安装java和Tomcat环境
前言 linux上安装软件,可以用yum非常方便,不需要下载解压,一个指令就能用yum安装java和tomcat环境. 前面一篇已经实现在阿里云服务器上搭建一个禅道系统的网站,算是小有成就,但并不是每 ...
- [mybatis-spring] Transaction 事务/事务处理/事务管理器
使用mybatis-spring的主要原因之一就是: mybatis-spring允许mybatis参与到spring 事务中. mybatis-spring leverage[use (someth ...
- javascript获取id元素
function $(id){ return document.getElementById(id); }导致所有的js不能用解决办法.... function $(id){ return doc ...
- DPDK 16.04/16.11.2 默认tx offload是关闭的引起tx vlan offload无效
打开IXGBE调试日志发发现:tx使用ixgbe_xmit_pkts_vec,默认tx offload无效了PMD: ixgbe_set_tx_function(): Using simple tx ...