环境依赖

创建一个新的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; @Autowired
public Receiver(CountDownLatch latch) {
this.latch = latch;
} public void receiveMessage(String message) {
LOGGER.info("Received <" + message + ">");
latch.countDown();
}
}

  

注入消息接收者

@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步已经完成,所以只需注入消息监听容器即可:

@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方法:

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

  

源码来源

Spring Boot教程(一)在springboot中用redis实现消息队列的更多相关文章

  1. SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot14-redis-mq/ 本文出自方志朋的博客 这 ...

  2. 在springboot中用redis实现消息队列

    环境依赖 创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖: <dependency> <groupId&g ...

  3. Spring Boot 入门(八):集成RabbitMQ消息队列

    本片文章续<Spring Boot 入门(七):集成 swagger2>,关于RabbitMQ的介绍请参考<java基础(六):RabbitMQ 入门> 1.增加依赖 < ...

  4. 程序员DD 《Spring boot教程系列》补充

    最近在跟着程序员DD的Spring boot教程系列学习Spring boot,由于年代原因,Spring boot已经发生了一些变化,所以在这里进行一些补充. 补充的知识大多来自评论区,百度,Sta ...

  5. spring boot 教程(三)配置详解

    在大部分情况下,我们不需要做太多的配置就能够让spring boot正常运行.在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性. Spring Boot 支持多种外部配置方式 这些 ...

  6. Spring Boot教程(十六)属性配置文件详解(1)

    相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...

  7. 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能

    springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...

  8. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  9. sping+redis实现消息队列的乱码问题

    使用spring支持redis实现消息队列,参考官方样例:https://spring.io/guides/gs/messaging-redis/ 实现后在运行过程中发现消费者在接收消息时会出现乱码的 ...

随机推荐

  1. HDU-4507-吉哥系列故事-恨7不成妻

    题目描述 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 ...

  2. 怎么编写properties文件

    1. 注释 在properties中注释是采用#号开头的方式来进行注释的 2. 编写properties文件 在properties中,一行就是一个键值对,简单的理解就是一行可以保存一个变量,键和值之 ...

  3. javaweb:关于HttpServletRequest介绍 (转)

    出处:https://www.cnblogs.com/xdp-gacl/p/3798347.html 一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的 ...

  4. 30分钟Maven入门到精通

    Maven是近年来最流行的项目构建与管理工具.不仅简化了我们开发过程中对jar包依赖的导入,还对项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等所有构建过程进行了抽象和统一,方便 ...

  5. HBASE学习笔记(三)

    一. 1.预切割:在创建表的时候,预先对表进行region切割.切割线就是rowkey $hbase> create '] $hbase>create 'ns2:t3',SPLITS=&g ...

  6. 和IE6-IE8说拜拜 一段IE兼容HTML代码 针对IE版本写css

    通过这段html可以让你对特定的ie版本添加内容,只在特定版本ie展现,可以是javascript.css.html. <!--[if IE]> 这样使用IE浏览器(全部版本)的人都看得到 ...

  7. vue-Ie下踩坑20190820

    开发的vue项目终于告一段落,终于是可以部署在测试服务器给客户看了,期间也是遇到各式各样的坑. 1.有些bug在谷歌是在没问题的,在ie上就会暴露出来,因为其中一个模块设计多个类型资源的列表,所以写纯 ...

  8. 简单了解 node http(一)

    简单了解 node http 模块 文章记录了对http 模块的简单使用与理解. http 服务端 http 客户端 总结 1. http 服务端 先写个小例子 服务端: let http = req ...

  9. com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'PDT' is.......

    SpringBoot连接数据库的时候报错 java.sql.SQLException: The server time zone value 'PDT' is unrecognized or repr ...

  10. 2019-11-29-win10-uwp-轻量级-MVVM-框架入门-2.1.5.3199

    title author date CreateTime categories win10 uwp 轻量级 MVVM 框架入门 2.1.5.3199 lindexi 2019-11-29 10:16: ...