在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选项 ...
随机推荐
- Java三种代理模式:静态代理、动态代理和cglib代理
一.代理模式介绍 代理模式是一种设计模式,提供了对目标对象额外的访问方式,即通过代理对象访问目标对象,这样可以在不修改原目标对象的前提下,提供额外的功能操作,扩展目标对象的功能. 简言之,代理模式就是 ...
- PHP的session的实现机制
一.默认机制,用磁盘文件来实现PHP会话.php.ini配置:session.save_handler = files 1.session_start() A. session_start()是ses ...
- npm升级package.json依赖包到最新版本号
转载自:https://blog.csdn.net/syaivin/article/details/79388244?utm_source=blogxgwz1 1.安装: npm install -g ...
- 《剑指offer》第四十八题(最长不含重复字符的子字符串)
// 面试题48:最长不含重复字符的子字符串 // 题目:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子 // 字符串的长度.假设字符串中只包含从'a'到'z'的字符. #inclu ...
- 学习笔记39—笑谈FireFox标签不同步(IOS和Wiindows)
为了解决国内用户连接 全球同步服务器 困难的问题,火狐中国版推出了 全球服务 和 本地服务 两套服务. 这两套服务的账号和数据并不通用,并且只有中国版提供了切换功能,因此当你在同步过程中遇到“未知账号 ...
- java操作vaftpd实现上传、下载
1.配置文件conf/vsftpd.properties (我是单独写了一个配置文件,你可以直接写在application中) vsftpd.ip=192.168.**.** vsftpd.user= ...
- Eclipse Jee环境配置
最近下载了新的Eclipse Jee Neon版本,记录一下如何进行开发环境的配置. 1.下载必要的开发环境文件 ①下载Java SE Development Kit (简称JDK) ②下载Tomca ...
- Python 百分比计算
遇到计算百分比的情况,查了一下,有两种方式 具体实现方式见下面代码 # 方式1 格式化为float ,然后 处理成%格式, 需要对分子/分母 * 100如下, percentList.append(' ...
- 几种RAID介绍(总结)
概念 RAID是Redundent Array of Inexpensive Disks的缩写,简称为“磁盘阵列”.后来RAID中的字母I被改作了Independent,RAID就成了“独立冗余磁盘阵 ...
- 环境变量LD_LIBRARY_PATH的传递
http://bbs.chinaunix.net/thread-3680861-1-1.html execv明显没有传环境变量,execle或execve才会带在启动shell设置的LD_LIBRAR ...