Springboot redis多数据源过期监听案例

在上一篇Springboot redis多数据源案例中,我们实现了springboot下多数据源的案例。

本篇博客在此基础上,实现多数据源过期监听事件:

监听器配置类:

package com.xinx.redismultidatasource.config.redis;

import com.xinx.redismultidatasource.listener.RedisDb1ExpiredListener;
import com.xinx.redismultidatasource.listener.RedisDbExpiredListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * redis过期监听配置类
 *
 *      redis过期监听配置(如果未测试通过,则程序无法监听到redis key过期事件)
 *      找到redis.conf文件,修改其中的notify-keyspace-events ""为notify-keyspace-events "Ex"
 *
 * 		测试redis过期监听是否开启成功:
 *
 * 		连接远程redis:
 * 			redis-cli -h [redis服务器ip] -p [redis端口] -a [密码]
 * 		订阅监听事件:
 * 		    psubscribe __keyevent@0__:expired
 *
 * 			新启一个客户端:
 * 			setex test_key 3 test_value
 *
 * @author xinx
 * @date 2020/9/11 18:04
 */
@Configuration
public class RedisExpiredListenerConfig {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    @Qualifier("redisDbExpiredListener")
    private RedisDbExpiredListener redisDbExpiredListener;

    @Autowired
    private StringRedisTemplate stringRedisTemplate1;
    @Autowired
    @Qualifier("redisDb1ExpiredListener")
    private RedisDb1ExpiredListener redisDb1ExpiredListener;

    /**
     * 配置redis过期监听器
     *
     * @param listenerAdapter
     * @return
     */
    @Bean
    @Primary
    RedisMessageListenerContainer container(@Qualifier("listenerAdapter") MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(stringRedisTemplate.getConnectionFactory());
        // __keyevent@[redis库索引]__:expired
        container.addMessageListener(listenerAdapter, new PatternTopic("__keyevent@0__:expired"));
        return container;
    }

    @Bean("listenerAdapter")
    @Primary
    MessageListenerAdapter listenerAdapter() {
        return new MessageListenerAdapter(redisDbExpiredListener);
    }

    @Bean
    RedisMessageListenerContainer container1(@Qualifier("listenerAdapter1") MessageListenerAdapter listenerAdapter1) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(stringRedisTemplate1.getConnectionFactory());
        container.addMessageListener(listenerAdapter1, new PatternTopic("__keyevent@1__:expired"));
        return container;
    }

    @Bean("listenerAdapter1")
    MessageListenerAdapter listenerAdapter1() {
        return new MessageListenerAdapter(redisDb1ExpiredListener);
    }

}

过期事件监听器:

package com.xinx.redismultidatasource.listener;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * @author xinx
 * @date 2020/9/11 18:07
 */
@Component("redisDbExpiredListener")
public class RedisDbExpiredListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.out.println(this.getClass().getCanonicalName() + "监听key:" + message.toString());
        System.out.println(new String(bytes));
    }
}
package com.xinx.redismultidatasource.listener;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * @author xinx
 * @date 2020/9/11 18:07
 */
@Component("redisDb1ExpiredListener")
public class RedisDb1ExpiredListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.out.println(this.getClass().getCanonicalName() + "监听key:" + message.toString());
        System.out.println(new String(bytes));
    }
}

 

你可能感兴趣的:(spring,boot,redis,redis)