异步消息的方式有很多,这篇博客介绍如何使用redis进行发布订阅,

完成这个示例只需要三个文件

1.redis消息监听配置

@Configuration
public class RedisListenerConfig {
/**
* redis消息监听器容器
* 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器
* 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
* @param connectionFactory
* @param listenerAdapter
* @return
*/
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter
) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory); //可以添加多个 messageListener
container.addMessageListener(listenerAdapter, new PatternTopic("index")); return container;
} /**
* 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
* @param redisReceiver
* @return
*/
@Bean
MessageListenerAdapter listenerAdapter(RedisReceiver redisReceiver) {
System.out.println("消息适配器进来了");
return new MessageListenerAdapter(redisReceiver, "receiveMessage");
} //使用默认的工厂初始化redis操作模板
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
}

2.消息处理

@Service
public class RedisReceiver { public void receiveMessage(String message) {
System.out.println("消息来了:"+message);
//这里是收到通道的消息之后执行的方法
}
}

3.一个发送消息的定时器

//定时器
@EnableScheduling
@Component
public class TestSenderController {
@Autowired
private StringRedisTemplate stringRedisTemplate; //向redis消息队列index通道发布消息
@Scheduled(fixedRate = 3000)
public void sendMessage(){
stringRedisTemplate.convertAndSend("index",String.valueOf(Math.random()));
}
}

运行结果如图

本篇博客地址:https://gitee.com/zhao-baolin/redis-message

spring boot 使用redis进行发布订阅的更多相关文章

  1. 【spring boot】【redis】spring boot 集成redis的发布订阅机制

    一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...

  2. spring boot: 用redis的消息订阅功能更新应用内的caffeine本地缓存(spring boot 2.3.2)

    一,为什么要更新caffeine缓存? 1,caffeine缓存的优点和缺点 生产环境中,caffeine缓存是我们在应用中使用的本地缓存, 它的优势在于存在于应用内,访问速度最快,通常都不到1ms就 ...

  3. Spring Boot使用Redis进行消息的发布订阅

    今天来学习如何利用Spring Data对Redis的支持来实现消息的发布订阅机制.发布订阅是一种典型的异步通信模型,可以让消息的发布者和订阅者充分解耦.在我们的例子中,我们将使用StringRedi ...

  4. netty-socketio(二)整合redis实现发布订阅

    1.Redis 发布订阅 参考:https://www.runoob.com/redis/redis-pub-sub.html Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub ...

  5. 【redis】spring boot利用redis的Keyspace Notifications实现消息通知

    前言 需求:当redis中的某个key失效的时候,把失效时的value写入数据库. github: https://github.com/vergilyn/RedisSamples 1.修改redis ...

  6. Springboot+Redis(发布订阅模式)跨多服务器实战

    一:redis中发布订阅功能(http://www.redis.cn/commands.html#pubsub) PSUBSCRIBE pattern [pattern -]:订阅一个或者多个符合pa ...

  7. redis 实现发布订阅的功能

    redis 除了作为缓存的功能外还可以用作消息中间件的功能,这片博客主要是介绍一下 redis 整合spring 实现消息的发布和订阅功能: 1:redis依赖,依赖两个包,redis 包, spri ...

  8. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  9. redis的发布订阅模式

    概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer {     /* Pubsub */     // 字典,键为频道, ...

随机推荐

  1. 支付宝红包口令自动复制到剪贴板脚本js,安卓,IOS通用版

    有客户找到涛舅舅,要求开发一个可以自动支付宝红包口令的js脚本,经过大量探索和优化,目前此脚本功能已经测试成功! 预期效果: 只要来访用户在当前网页的任意位置点击一下,支付宝红包口令即可复制到用户手机 ...

  2. Chapter 5 : Control Structures 2 : Repetition

    import java.util.*; import java.io.*; public class Loop { static Scanner console = new Scanner(Syste ...

  3. mysql练习

    1.表关系 创建如下表格,并创建相关约束 ##(1)创建一个数据库 create database db2 default charset utf8; ##切换到db2数据库中 use db2 ##创 ...

  4. Android第三次作业

    制作音乐播放器 实现的功能: 歌曲的播放.暂停.停止.上一首.下一首.歌单列表的显示(获取本地歌曲). 成品图: 功能代码实现: 实现歌曲的播放.暂停.停止.上一首.下一首: public void ...

  5. Centos服务器上NFS灾备环境及KVM的搭建及使用

    1.概述 由于在单台服务器上搭建灾备环境需要KVM和NFS的支持,下面先列出KVM的搭建流程,再列出使用NFS实现单台服务器灾备的流程. A.搭建KVM环境 1>.主机环境准备 Linux Sy ...

  6. java 加解密

    import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingExc ...

  7. Python入门—文件读写

    文件读写的基本流程: #1.打开文件#2.读写文件#3.关闭文件 f = open('文件读写',encoding='utf-8') #打开文件,并赋值给f,encoding='utf-8'让中文可以 ...

  8. Springboot Selenide UI 自动化测试

    标题中的Selenide 并没有拼错,确实不是selenium Selenium做UI自动化,可以参考我其他的blog: Selenium做自动化最好要封装起来,否则对于元素的等待,页面的加载会使得自 ...

  9. [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  10. [Swift]LeetCode483. 最小好进制 | Smallest Good Base

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...