通过spring搭建的一个redis监听器,用于固定时间进行任务处理

1. RedisKeys 类

bash 复制代码
package com.cqcloud.platform.constants;

/**
 * @author weimeilayer@gmail.com ✨
 * @date 💓💕 2024年10月21日 🐬🐇 💓💕
 */
public class RedisKeys {

    /**
     * redis文件夹
     */
    public static final String COLON = ":";
    /**
     * 系统公共参数
     */
    public static final String SYS_PUBLIC_PARAM_LIST = "SysPublicParamList";
    /**
     * 行政区划(根据PID)
     */
    public static final String DIC_CITY_INFO = "DicCityInfo";
    /**
     * 行政区划(根据全程)
     */
    public static final String DIC_CITY_INFO_FOR_NAME = "DicCityInfoForName";
    /**
     * 设备在线状态
     */
    public static final String DEVICE_ONLINE_STATUS = "DeviceOnlineStatus";
    /**
     * 微信开放平台的唯一标识
     */
    public static final String WX_UNION_ID = "WxUnionId";
    /**
     * 微信开放平台的唯一标识(刷新)
     */
    public static final String WX_UNION_ID_REFRESH = "WxUnionIdRefresh";
}

2. KeyExpiredListener 类

bash 复制代码
package com.cqcloud.platform.config;

import com.cqcloud.platform.constants.RedisKeys;
import lombok.extern.slf4j.Slf4j;
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;
/**
 * redis Key过期监听
 * @author weimeilayer@gmail.com ✨
 * @date 💓💕 2024年9月28日 🐬🐇 💓💕
 */
@Slf4j
@Component
public class KeyExpiredListener extends KeyExpirationEventMessageListener {

    public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message) {
        String expiredKey = message.toString();
        log.info("Expired key: {}", expiredKey);

        if (expiredKey.startsWith(RedisKeys.DEVICE_ALARM_RECORD + RedisKeys.COLON)) {
            String id = expiredKey.substring((RedisKeys.DEVICE_ALARM_RECORD + RedisKeys.COLON).length());
            log.info("Alarm record expired: {}", id);
            // 这里可以注入服务并处理过期逻辑
        }

        if (expiredKey.startsWith(RedisKeys.WX_UNION_ID_REFRESH + RedisKeys.COLON)) {
            String unionId = expiredKey.substring((RedisKeys.WX_UNION_ID_REFRESH + RedisKeys.COLON).length());
            log.info("WX Union ID refreshed: {}", unionId);
            // 这里可以注入服务并处理过期逻辑
        }
    }
}

3. RedisConfiguration 类

/**

bash 复制代码
package com.cqcloud.platform.config;

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;

@Configuration
public class RedisConfiguration {

    private final RedisConnectionFactory redisConnectionFactory;

    public RedisConfiguration(RedisConnectionFactory redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory;
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        return container;
    }

    @Bean
    public KeyExpiredListener keyExpiredListener() {
        return new KeyExpiredListener(redisMessageListenerContainer());
    }
}
相关推荐
翻滚丷大头鱼7 分钟前
Java 集合Collection—List
java·开发语言
sanggou16 分钟前
License 集成 Spring Gateway:解决 WebFlux 非阻塞与 Spring MVC Servlet 阻塞兼容问题
spring·gateway·mvc
敲键盘的肥嘟嘟左卫门43 分钟前
StringBuilder类的数据结构和扩容方式解读
java
索迪迈科技1 小时前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法
十碗饭吃不饱1 小时前
net::ERR_EMPTY_RESPONSE
java·javascript·chrome·html5
白初&1 小时前
SpringBoot后端基础案例
java·spring boot·后端
哈基米喜欢哈哈哈1 小时前
ThreadLocal 内存泄露风险解析
java·jvm·面试
萌新小码农‍2 小时前
Java分页 Element—UI
java·开发语言·ui
鼠鼠我捏,要死了捏2 小时前
Redis缓存穿透、缓存击穿与雪崩防护及性能优化实战指南
redis·cache·performance
鼠鼠我捏,要死了捏2 小时前
深入实践G1垃圾收集器调优:Java应用性能优化实战指南
java·g1·gc调优