news 2026/2/9 9:18:40

Redis拓展 - 实现定时消息通知

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Redis拓展 - 实现定时消息通知
1. Redis实现定时消息通知

简单定时任务通知: 利用redis的keyspace notifications(即:键过期后事件通知机制)

  • 开启方法
    • 修改server.conf文件,找到notify-keyspace-events , 修改为“Ex”
    • 使用cli命令:redis-cli config set notify-keyspace-events Ex
  • redis 配置参考


2. 例子

创建springboot项目

  1. 修改pom.xml 和 yml
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.9</version> <!-- 这个版本其实还是挺重要的,如果是2.7.3版本的话,大概会无法成功自动装载 RedisConnectionFactory --> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.lazyfennec</groupId> <artifactId>redisdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redisdemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
  • application.yml
spring: redis: database: 0 host: 192.168.1.7 port: 6379 jedis: pool: max-active: 8 max-idle: 8 min-idle: 0 max-wait: 1
  1. 创建RedisConfig
package cn.lazyfennec.redisdemo.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @Author: Neco * @Description: * @Date: create in 2022/9/20 23:19 */ @Configuration @EnableCaching public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { // 设置序列化 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); // key 序列化 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // value 序列化 redisTemplate.setHashKeySerializer(stringSerializer); // Hash key 序列化 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // Hash value 序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } }
  1. 创建RedisListenerConfiguration
package cn.lazyfennec.redisdemo.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.RedisMessageListenerContainer; /** * @Author: Neco * @Description: * @Date: create in 2022/9/21 11:02 */ @Configuration public class RedisListenerConfiguration { @Autowired private RedisConnectionFactory factory; @Bean public RedisMessageListenerContainer redisMessageListenerContainer() { RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer(); redisMessageListenerContainer.setConnectionFactory(factory); return redisMessageListenerContainer; } }
  1. 事件监听事件 RedisTask
package cn.lazyfennec.redisdemo.task; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.stereotype.Component; import java.nio.charset.StandardCharsets; /** * @Author: Neco * @Description: * @Date: create in 2022/9/21 11:05 */ @Component public class RedisTask extends KeyExpirationEventMessageListener { public RedisTask(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { // 接收到事件后回调 String channel = new String(message.getChannel(), StandardCharsets.UTF_8); String key = new String(message.getBody(), StandardCharsets.UTF_8); System.out.println("key:" + key + ", channel:" + channel); } }
  1. 发布 RedisPublisher
package cn.lazyfennec.redisdemo.publisher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Random; import java.util.concurrent.TimeUnit; /** * @Author: Neco * @Description: * @Date: create in 2022/9/21 16:34 */ @Component public class RedisPublisher { @Autowired RedisTemplate<String, Object> redisTemplate; // 发布 public void publish(String key) { redisTemplate.opsForValue().set(key, new Random().nextInt(200), 10, TimeUnit.SECONDS); } // 循环指定时间触发 @Scheduled(cron = "0/15 * * * * ?") public void scheduledPublish() { System.out.println("scheduledPublish"); redisTemplate.opsForValue().set("str1", new Random().nextInt(200), 10, TimeUnit.SECONDS); } }
  1. 要实现Scheduled需要在启动类上加上注解
@SpringBootApplication @EnableScheduling // 要加上这个,用以启动 public class RedisdemoApplication {
  1. 修改TestController
package cn.lazyfennec.redisdemo.controller; import cn.lazyfennec.redisdemo.publisher.RedisPublisher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; /** * @Author: Neco * @Description: * @Date: create in 2022/9/21 16:32 */ @RestController public class TestController { @Autowired RedisPublisher redisPublisher; @GetMapping("/redis/{key}") public String publishEvent(@PathVariable String key) { // 发布事件 redisPublisher.publish(key); return "OK"; } }

如果觉得有收获,欢迎点赞和评论,更多知识,请点击关注查看我的主页信息哦~

AI大模型学习福利

作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获取

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。


因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

四、AI大模型商业化落地方案

因篇幅有限,仅展示部分资料,需要点击文章最下方名片即可前往获

作为普通人,入局大模型时代需要持续学习和实践,不断提高自己的技能和认知水平,同时也需要有责任感和伦理意识,为人工智能的健康发展贡献力量

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/7 13:51:27

Python Victor-DAY 8 标签编码与连续变量处理

知识点复盘&#xff1a; 字典的简单介绍&#xff08;增删查改&#xff09;标签编码&#xff08;字典的映射&#xff09;对独热编码的深入理解----n个不相关变量只有n-1个自由的连续特征的处理&#xff1a;归一化和标准化----一般选一个即可&#xff0c;谁好谁坏做了才知道&…

作者头像 李华
网站建设 2026/2/9 7:34:20

Open-AutoGLM应用十大场景(90%的企业还不知道的自动化红利)

第一章&#xff1a;Open-AutoGLM 跨境贸易自动化在跨境贸易场景中&#xff0c;信息异构、流程复杂和多语言沟通障碍长期制约着效率提升。Open-AutoGLM 作为一款基于开源大语言模型的自动化框架&#xff0c;专为解决此类问题而设计&#xff0c;能够实现订单处理、报关文档生成、…

作者头像 李华
网站建设 2026/2/4 8:56:00

网络安全入门知识地图:快速构建你的第一层防御体系(新手不绕路)

当我们学习网络安全的时候&#xff0c;需要对它的基础知识做一个简单的了解&#xff0c;这样对以后的学习和工作都会有很大的帮助。本篇文章为大家总结了网络安全基础知识入门的内容&#xff0c;快跟着小编来学习吧。 计算机网络 计算机网络是利用通信线路将不同地理位置、具…

作者头像 李华
网站建设 2026/2/8 8:54:04

零经验想跳槽转行网络安全,需要准备什么?(详细版)

前言 最近在后台收到了部分私信&#xff0c;大部分都是关于网络安全转行的问题&#xff0c;其中&#xff0c;目前咨询最多的是&#xff1a;觉得现在的工作没有发展空间&#xff0c;替代性强&#xff0c;工资低&#xff0c;想跳槽转行网络安全。其中&#xff0c;大家主要关心的…

作者头像 李华