Springboot redisマルチデータ・ソースの期限切れリスニング・ケース


前のSpringboot redisマルチデータソースの例では、springbootの下でマルチデータソースの例を実現しました.
このブログに基づいて、マルチデータ・ソースの期限切れのリスニング・イベントを実現します.
Listener構成クラス:
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));
    }
}