栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot 项目spring -data-redis监听key过期事件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot 项目spring -data-redis监听key过期事件

当我们需要动态的根据开始时间或者结束时间来改变某个字段的状态时,我们可以使用

JedisUtil.set(key,subTask.getId(),expireTime.intValue());

根据 springDataRedis来获取key失效的时间,去动态更新字段的状态

1.配置Redis监听配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;


@Configuration
public class RedisListenerConfig {

    @Value("${spring.redis.database}")
    private Integer db;

    @Bean
    public RedisMessageListenerContainer customizeRedisListenerContainer(
            RedisConnectionFactory redisConnectionFactory,MessageListener messageListener) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(messageListener,new PatternTopic("__keyevent@"+db.intValue()+"__:expired"));
        return redisMessageListenerContainer;
    }
}

2.监听key过期的业务实现类  onMessage中可以写自己的业务代码

import com.lydata.handfulcode.business.train.constant.BusinessConstant;
import com.lydata.handfulcode.business.train.service.ILearnTaskUserRecordService;
import com.lydata.handfulcode.common.redis.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@Component
public class KeyExpiredEventMessageListener implements MessageListener {

    @Autowired
    private ILearnTaskUserRecordService recordService;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        log.info("监听失效的redisKey:{},值是:{}, {}", message.toString(), new String(message.getBody()), new String(bytes));
        String expireKey = message.toString();
        if (expireKey.startsWith(BusinessConstant.REDIS_KEY_PRE) || expireKey.startsWith(BusinessConstant.REDIS_KEY_START_TIME)) {
            String id = expireKey.split(":")[1];
            Long subTaskId = Long.valueOf(id);
            if (expireKey.startsWith(BusinessConstant.REDIS_KEY_PRE)) {
                recordService.updateTaskUserInfoStatus(subTaskId);
            } else {
                recordService.updateStatusByStartTimeExpire(subTaskId);
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966774.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号