为什么要自定义事件监听呢?当然是为了在redis进行某些事件动作的时候增加一些咱们自己的业务逻辑处理,比如:当key失效的时候,处理业务逻辑--支付订单半小时未支付就自动取消订单。这就可以用key失效事件监听来实现。下面就来讲解怎么实现:
1.首先自定义一个消息工厂此处不要自己再重新获取连接自定义连接池。直接使用redis现有的连接池即可。
package com.liu.redisexpired; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; 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; import reactor.util.annotation.NonNull; @Slf4j public class RedisMessageListenerFactory implements BeanFactoryAware, ApplicationListener2.将消息工厂注入spring容器{ //spring bean工厂 private DefaultListableBeanFactory beanFactory; //redis连接工厂 private RedisConnectionFactory redisConnectionFactory; //redis消息监听 @Autowired private MessageListener messageListener; public void setBeanFactory(@NonNull BeanFactory beanFactory) { this.beanFactory = (DefaultListableBeanFactory) beanFactory; } public void setRedisConnectionFactory(RedisConnectionFactory redisConnectionFactory) { this.redisConnectionFactory = redisConnectionFactory; } @Override public void onApplicationEvent(@NonNull ContextRefreshedEvent contextRefreshedEvent) { //定义一个单例bean,设置连接工厂为redis的连接工厂,然后注册到spring容器 BeanDefinitionBuilder containerBeanDefinitionBuilder = BeanDefinitionBuilder .genericBeanDefinition(RedisMessageListenerContainer.class); containerBeanDefinitionBuilder.addPropertyValue("connectionFactory", redisConnectionFactory); containerBeanDefinitionBuilder.setScope(BeanDefinition.SCOPE_SINGLETON); containerBeanDefinitionBuilder.setLazyInit(false); beanFactory.registerBeanDefinition("myRedisKeyListenerContainer", containerBeanDefinitionBuilder.getRawBeanDefinition()); //从spring工厂容器获取消息监听容器,配置消息监听器 RedisMessageListenerContainer container = beanFactory.getBean("myRedisKeyListenerContainer", RedisMessageListenerContainer.class); //设置监听正则,监听节点所有key的所有事件 container.addMessageListener(messageListener, new PatternTopic("__keyevent@*__:*")); container.afterPropertiesSet(); //开始监听 container.start(); } }
在你的redisconfig配置中加入代码,主要看最后一个方法:
package com.liu.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.liu.redisexpired.RedisMessageListenerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean("redisTemplate")
@Primary
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
return getRedisTemplate(redisConnectionFactory);
}
private RedisTemplate getRedisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer
3.编写消息监听:
编写一个监听器,实现MessageListener接口,实现onMessage方法,得到的message就可以监听所有的key的事件了。此处只监听了key的过期事件,然后打印了一句日志,如果需要其他操作,可直接在此处改写即可
package com.liu.redisexpired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class KeyEventMessageListener implements MessageListener {
@Override
public void onMessage(Message message, byte[] pattern) {
//获取redis操作类型: set、expire、expired等操作
String action = new String(message.getChannel());
action = action.split(":")[1];
//获取redis操作的key
String key = new String(message.getBody());
//判断失效的key,且是请求限流的key
if("expired".equals(action) && key.startsWith("request_limit_")){
log.info("监听到的键:{},已经失效,请求限流已取消!", key);
}
}
}
此处作者监听的是以request_limit_开头的键失效事件(做接口限流的时候,顺便整了一下键事件监听)。
4. 然后验证:启动你的项目,然后使用你的项目设置一个键并设置失效事件;
或者直接连接redis命令行,先通过如下命令连接redis命令行:
redis-cli -h 127.0.0.1 -p 6379 -a xxx
在命令行中执行命令添加一个键值,并设置过期时间为2秒:
set request_limit_test 1 PX 2000
然后查看项目的控制台是否有对应的输出:
以上就是所有内容



